Jump to content
Serge_G

TListBox, TListView how to use Sort(Compare) ?

Recommended Posts

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

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

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
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 by Remy Lebeau

Share this post


Link to post
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

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)Capture.thumb.PNG.6e0600fa31c0919363d9defef608de54.PNG

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×