Recent

Author Topic: [SOLVED] Help with TFPGMAP sort...  (Read 1032 times)

Espectr0

  • Full Member
  • ***
  • Posts: 218
[SOLVED] Help with TFPGMAP sort...
« on: April 30, 2022, 11:07:47 pm »
Hello,
I have problems to order a TFPGMAP according to the DATA value:

type:
Code: Pascal  [Select][+][-]
  1. TTMXList = specialize TFPGMap<Integer, Double>;

data compare:
Code: Pascal  [Select][+][-]
  1. function DataCompare(const Data1, Data2: Double): Integer;
  2. begin
  3.   if Data1 < Data2 then
  4.     Result := -1
  5.   else if Data1 > Data2 then
  6.     Result := 1
  7.   else
  8.     Result := 0;
  9. end;            

Try the 'Sorted := True' property and 'Sort' but I can't get it to order.

What am I doing wrong?
thanks
« Last Edit: May 01, 2022, 02:31:34 pm by Espectr0 »

bytebites

  • Hero Member
  • *****
  • Posts: 639
Re: Help with TFPGMAP sort...
« Reply #1 on: May 01, 2022, 09:19:55 am »
...
What am I doing wrong?

You provided only code snippets.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode delphi}
  3. uses fgl;
  4. type
  5.   tmap=tfpgmap<integer,double>;
  6. var
  7.   m:tmap;
  8.   i: Integer;
  9. begin
  10.   m:=tmap.create;
  11.   m.Sorted:=true;
  12.   m.Add(1,1.1);
  13.   m.Add(8,4.2);
  14.   m.Add(2,2.3);
  15.   m.Add(0,0.4);
  16.   for i:=0 to m.Count-1 do writeln(m.Data[i]:4:1);
  17. end.      
  18.  
Output is
 0.4
 1.1
 2.3
 4.2

JdeHaan

  • Full Member
  • ***
  • Posts: 118
Re: Help with TFPGMAP sort...
« Reply #2 on: May 01, 2022, 12:13:40 pm »
I believe sorting is done on the key only if you set sorted to true...

Espectr0

  • Full Member
  • ***
  • Posts: 218
Re: Help with TFPGMAP sort...
« Reply #3 on: May 01, 2022, 01:17:09 pm »
Thanks @bytebites but @JdeHaan is right, the sort is based on the "KEY", so I tried "OnDataCompare" but I can't get it to work that way, any ideas?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Help with TFPGMAP sort...
« Reply #4 on: May 01, 2022, 02:11:00 pm »
Maps can only sort on the key, they are simply not designed to sort for data. Either use another datastructure or sort the data yourself.

I use this opportunity to plug my iterator library, which allows this:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode Delphi}
  4. {$ModeSwitch implicitfunctionspecialization}
  5.  
  6. uses
  7.   Generics.Collections, iterators;
  8.  
  9. type
  10.   TIntDoubleMap = TDictionary<Integer, Double>;
  11.   TIntDoublePair = TPair<Integer, Double>;
  12.  
  13. function ComparePairValue(A, B: TIntDoublePair): Boolean;
  14. begin
  15.   Result := A.Value < B.Value;
  16. end;
  17.  
  18. var
  19.   data: TIntDoubleMap;
  20.   p: TIntDoublePair;
  21. begin
  22.   data := TIntDoubleMap.Create;
  23.   try
  24.     data.Add(1, 5);
  25.     data.Add(2, 3);
  26.     data.Add(4, 6);
  27.     data.Add(10, 1);
  28.     for p in Sorted(Iterate(data), ComparePairValue) do
  29.       WriteLn(p.Value);
  30.   finally
  31.     data.Free;
  32.   end;
  33.   ReadLn;
  34. end.
  35.  
« Last Edit: May 01, 2022, 02:15:24 pm by Warfley »

Espectr0

  • Full Member
  • ***
  • Posts: 218
[SOLVED] Help with TFPGMAP sort...
« Reply #5 on: May 01, 2022, 02:31:06 pm »
Thanks @Warfley, I'm going to investigate your library, I find it very interesting. Regards!

 

TinyPortal © 2005-2018