how to sort in Tlistview based on subitem[x]
Asked Answered
D

1

8

How to sort in tlistview with data present in subitem[x]?

Damask answered 3/7, 2010 at 16:11 Comment(0)
G
10

Set SortType := stData and write

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
begin
  Compare := StrToInt(Item1.SubItems[x]) - StrToInt(Item2.SubItems[x])
end;

for instance. If compare is negative, Item1 should come before Item2; if compare is positive, the opposite applies. Thus this example, which assumes that SubItem[x] contains an integer, will sort the items according to the numerical value of SubItem[x].

If, on the other hand, SubItem[x] contains strings, then you can write

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
begin
  Compare := AnsiCompareText(Item1.SubItems[x], Item2.SubItems[x]);
end;
Graviton answered 3/7, 2010 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.