Serge_G 87 Posted April 28, 2020 Hi, I found a way to sort my lists using Sort an OnCompare event but I don't figure out a functional way to use Sort(Compare) method (Rio 10.3.3) Any clues ? Thanks Share this post Link to post
Remy Lebeau 1394 Posted April 28, 2020 Can you be more specific? What exactly is not working for you? Please show the code you are having trouble with. Share this post Link to post
Vandrovnik 214 Posted April 28, 2020 You mean something like this? type tSledovaniVykonuSeznamVysledku=class(tObjectList<tSledovaniVykonuVysledky>) ... var Cmp: IComparer<tSledovaniVykonuVysledky>; begin Cmp:=tDelegatedComparer<tSledovaniVykonuVysledky>.Create( function(const Left, Right: tSledovaniVykonuVysledky): Integer begin result:=CompareStr(Left.Nazev, Right.Nazev); end); Sort(Cmp); Share this post Link to post
Remy Lebeau 1394 Posted April 28, 2020 (edited) 1 hour ago, Vandrovnik said: You mean something like this? What does that code have to do with TListBox/TListView? Their Sort() methods are quite different than TObjectList's Sort() method. Edited April 28, 2020 by Remy Lebeau Share this post Link to post
Vandrovnik 214 Posted April 28, 2020 4 minutes ago, Remy Lebeau said: What does that code have to do with TListBox/TListView? Their Sort() methods are quite different than TObjectList's Sort() method. Sorry, I did not read carrefully the title. Share this post Link to post
Serge_G 87 Posted April 29, 2020 Sorry, not being specific. Late I found, really by chance, why the code I used procedure TForm110.btnDetailClick(Sender: TObject); // numeric sort on detail var Compare : TFMXObjectSortCompare; begin Tri:=btnDetail.Tag; if btnDetail.Tag=1 then btnDetail.Tag:=0 else btnDetail.Tag:=1; ListBox1.Sorted:=False; Compare:=function(item1, item2: TFmxObject): Integer var n1,n2 : Single; begin n1:=StrToFloatDef(TListBoxItem(item1).ItemData.Detail,0); n2:=StrToFloatDef(TListBoxItem(item2).ItemData.Detail,0); if n2=n1 then result:=0 else begin if n2>n1 then Result := 1 else Result := -1; end; if tri=0 then result:=result*-1; end; ListBox1.Sort(Compare); // here end; seems to be useless. Just resizing the form (to take a picture) 'refreshed' the aligned list. Before solving, I used a BeginUpdate/EndUpdate sequence with no effect ! So that was why I don't understood the usage of list.sort(compare) was not 'working'. A self.width:=self.width+1; self.width:=self.width; confirmed that but was not the solution. Finally, digging in the source code, I found the solution ListBox1.RealignContent; Just after the sort did it. Joined, comparative runtime screens (first alpha sort on text using sorted:=true, second numeric sort on detail using sort(compare) twice to have DESC order) 1 Share this post Link to post