Forum > General

[SOLVED] Help with TFPGMAP sort...

(1/2) > >>

Espectr0:
Hello,
I have problems to order a TFPGMAP according to the DATA value:

type:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TTMXList = specialize TFPGMap<Integer, Double>;
data compare:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function DataCompare(const Data1, Data2: Double): Integer;begin  if Data1 < Data2 then    Result := -1  else if Data1 > Data2 then    Result := 1  else    Result := 0;end;            
Try the 'Sorted := True' property and 'Sort' but I can't get it to order.

What am I doing wrong?
thanks

bytebites:

--- Quote from: Espectr0 on April 30, 2022, 11:07:47 pm ---...
What am I doing wrong?

--- End quote ---

You provided only code snippets.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$mode delphi}uses fgl;type  tmap=tfpgmap<integer,double>;var  m:tmap;  i: Integer;begin  m:=tmap.create;  m.Sorted:=true;  m.Add(1,1.1);  m.Add(8,4.2);  m.Add(2,2.3);  m.Add(0,0.4);  for i:=0 to m.Count-1 do writeln(m.Data[i]:4:1);end.       Output is
 0.4
 1.1
 2.3
 4.2

JdeHaan:
I believe sorting is done on the key only if you set sorted to true...

Espectr0:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode Delphi}{$ModeSwitch implicitfunctionspecialization} uses  Generics.Collections, iterators; type  TIntDoubleMap = TDictionary<Integer, Double>;  TIntDoublePair = TPair<Integer, Double>; function ComparePairValue(A, B: TIntDoublePair): Boolean;begin  Result := A.Value < B.Value;end; var  data: TIntDoubleMap;  p: TIntDoublePair;begin  data := TIntDoubleMap.Create;  try    data.Add(1, 5);    data.Add(2, 3);    data.Add(4, 6);    data.Add(10, 1);    for p in Sorted(Iterate(data), ComparePairValue) do      WriteLn(p.Value);  finally    data.Free;  end;  ReadLn;end. 

Navigation

[0] Message Index

[#] Next page

Go to full version