IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

trier des paires (lettre & entier) par ordre croissant des entiers associés (Delphi10)


Sujet :

Langage Delphi

  1. #21
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 119
    Points : 41 252
    Points
    41 252
    Billets dans le blog
    63
    Par défaut
    Au temps pour moi, un TDictionnary ne peut être trié. Toutefois, il y a des possibilités pour en faire une lecture en ordre selon les valeurs ou les clés en passant par un Tarray

    Quelque chose comme

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    uses
      Generics.Collections, Generics.Defaults, SysUtils;
     
    public 
      LDict : TDictionary<integer, String>;
     
    function lettresasaisir(Adictionnary : TDictionary<Integer,String>, nombrelettres : integer) : String;
     var
      LArray : TArray<integer>; 
      i,j : integer;
    begin
      LArray := Adictionnary.Keys.ToArray;
      TArray.Sort<integer>(LArray); // trier
     
    result:=EmptyStr;
    j:=0;
    for i in Larray do 
    begin 
     result:=result+aDictionnary.items[i];
     // modifier le poids lettre aDictionnary 
     inc(j);  
     if j=nombrelettres then break;
    end;
    end;

  2. #22
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 586
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 586
    Points : 25 262
    Points
    25 262
    Par défaut
    En dehors que le code de SergioMaster ne compile pas, au final vu que cela prend chaque lettre une par une sans aléatoire, pour assurer la rotation des lettres, il faut un dictionnaire de lettre plus long que le nombre de lettres à choisir (et non multiple)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    function lettresasaisir(Adictionnary : TDictionary<Char, Integer>; nombrelettres : integer) : String;
     var
      LArray : TArray<Integer>;
      i,j : integer;
      L: TPair<Char, Integer>;
    begin
      LArray := Adictionnary.Values.ToArray;
      TArray.Sort<Integer>(LArray); // trier
     
      result:=EmptyStr;
      j:=0;
      for i in Larray do
      begin
        for L in Adictionnary do
        begin
          if L.Value = I then
          begin
            result:=result+L.Key;
            aDictionnary.Items[L.Key] := aDictionnary.Items[L.Key] + 1;
            inc(j);
            if j=nombrelettres then
             Exit;
          end;
        end;
      end;
    end;
     
    procedure TForm1.Button2Click(Sender: TObject);
    var
      I: Integer;
      Ls: TDictionary<Char, Integer>;
      L: TPair<Char, Integer>;
    begin
      Ls := TDictionary<Char, Integer>.Create();
      try
        for I := 1 to Length(Edit1.Text) do
          Ls.Add(Edit1.Text[I], 0);
     
        for I := 0 to SpinEdit1.Value - 1 do
          ListBox1.Items.Add(lettresasaisir(Ls, SpinEdit2.Value));
     
        for L in Ls do
          ListBox2.Items.Add(Format('%s: %d', [L.Key, L.Value]));
      finally
        Ls.Free();
      end;
    end;
    Nom : Sans titre.jpg
Affichages : 138
Taille : 80,7 Ko





    Au lieu du Sort<Integer>, faudrait voir si l'on peut fourni un TComparer dans un Sort<TPair<Char, Integer>>

    Dans un besoin d'un Tri par quantité, je l'ai fait avec un TComparer mais niveau syntaxe, faut s'accrocher, c'est similaire à la proposition de MatMagik

    Citation Envoyé par ShaiLeTroll Voir le message

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
      TAutomateLogistiqueBusinessEngine = class(TObject)
      strict private
        type
          TDestinationList = System.Generics.Collections.TObjectDictionary<string, TDestinationInfo>;
          TDestinationListIterator = System.Generics.Collections.TPair<string, TDestinationInfo>;
          TDestinationListOrderer = class(TComparer<TDestinationListIterator>)
          public
            function Compare(const Left, Right: TDestinationListIterator): Integer; override;
          end;
    ...
     
     
    //------------------------------------------------------------------------------
    function TAutomateLogistiqueBusinessEngine.BuildMapTriFromSession(... bla bla ...): Boolean;
    var
      SessionDestinationList: TDestinationList;
      OrderedDestinations: TArray<TDestinationListIterator>;
     
      // * Phase 1 - Récupération des destinations FINALES !
      SessionDestinationList := TDestinationList.Create([doOwnsValues]);
      try
         ... remplissage de SessionDestinationList ...
     
     
        // Calcul du plan selon la quantité
        OrderedDestinations := SessionDestinationList.ToArray();
        DestinationListOrderer := TDestinationListOrderer.Create();
        try
          TArray.Sort<TDestinationListIterator>(OrderedDestinations, DestinationListOrderer);
        finally
          DestinationListOrderer.Free();
        end;
     
     
    ...
     
    { TAutomateLogistiqueBusinessEngine.TDestinationListOrderer }
     
    //------------------------------------------------------------------------------
    function TAutomateLogistiqueBusinessEngine.TDestinationListOrderer.Compare(const Left, Right: TDestinationListIterator): Integer;
    begin
      // Le résultat est inférieur à zéro (<0) : Left est inférieur à Right : Left est plus contient PLUS que Right
      // Le résultat est égal à zéro (=0)      : Left est égal à Right.
      // Le résultat est supérieur à zéro (>0) : Left est supérieur à Right : Left est plus contient MOINS que Right
      // Je veux les plus GROS en premier
      Result := Right.Value.Quantity - Left.Value.Quantity;
    end;
    ...

  3. #23
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 586
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 586
    Points : 25 262
    Points
    25 262
    Par défaut
    La syntaxe, j'avais prévenu, c'est rugueux :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    function lettresasaisir(Adictionnary : TDictionary<Char, Integer>; nombrelettres : integer) : String;
    var
      LArray : TArray<TPair<Char, Integer>>;
      j : integer;
      L: TPair<Char, Integer>;
    begin
      LArray := Adictionnary.ToArray();
      TArray.Sort<TPair<Char, Integer>>(LArray, TComparer<TPair<Char, Integer>>.Construct(
          function(const Left, Right: TPair<Char, Integer>): integer
          begin
            Result := Left.Value - Right.Value;
          end));
     
      result:=EmptyStr;
      j:=0;
      for L in Larray do
      begin
        result:=result+L.Key;
        aDictionnary.Items[L.Key] := aDictionnary.Items[L.Key] + 1;
        inc(j);
        if j=nombrelettres then
          Exit;
      end;
    end;
    Je recommande de créer des type TLetterMap, TLetterIterator et TLetterComparer pour une syntaxe plus conventionnel soit respectivement dans mon précédent exemple TDestinationList, TDestinationListIterator et TDestinationListOrderer




    Donc maintenant, suffit de mélanger ce TDictionary<Char, Integer> en appliquant l'algo aléatoire avec array of TLetter et LetterPonderation comme j'ai fourni sur la page précédente

  4. #24
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Août 2005
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 31
    Points : 24
    Points
    24
    Par défaut
    merci à vous, z'êtes trop forts !!

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Trier des données par ordre croissant
    Par texas2607 dans le forum Macros et VBA Excel
    Réponses: 19
    Dernier message: 30/04/2020, 14h04
  2. Trier des combobox par ordre croissant et alphabétique
    Par floflo50100 dans le forum Excel
    Réponses: 6
    Dernier message: 09/04/2015, 15h31
  3. Trier par ordre croissant des valeurs dans un tableau
    Par ftrap dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 26/06/2013, 10h35
  4. [VBA]Trier les valeur d une liste par ordre croissant
    Par PierrotKun dans le forum VBA Access
    Réponses: 1
    Dernier message: 30/03/2007, 09h37
  5. Trier un tableau par ordre croissant
    Par Halleck dans le forum Algorithmes et structures de données
    Réponses: 15
    Dernier message: 01/11/2004, 00h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo