Recent

Author Topic: TListView doesn't have a sorted property  (Read 10503 times)

glober

  • Full Member
  • ***
  • Posts: 130
TListView doesn't have a sorted property
« on: April 21, 2005, 04:34:41 am »
How does one sort TListView items?   Delphi has "Sorted" and "SortDirection".

The "SortType" property doesn't seem to do anything?

Also clicking on the title bars doesn't sort anything?

Maybe I should be posting this where the developer guys hang out. :)

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2512
TListView doesn't have a sorted property
« Reply #1 on: April 21, 2005, 11:23:18 am »
to be implemented.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

matthijs

  • Hero Member
  • *****
  • Posts: 537
Re: TListView doesn't have a sorted property
« Reply #2 on: April 21, 2005, 11:40:17 am »
Quote from: "glober"
Also clicking on the title bars doesn't sort anything?

Clicking the title bar doesnot sort in Delphi either.
The developer guys come here as well. :)
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

glober

  • Full Member
  • ***
  • Posts: 130
Re: TListView doesn't have a sorted property
« Reply #3 on: April 22, 2005, 02:26:06 am »
Quote from: "matthijs"

Clicking the title bar doesnot sort in Delphi either.


Yes, true, it would have been more proper for me to say that clicking on the title bar doesn't change sort direction or which column to sort by when sorted=true.

matthijs

  • Hero Member
  • *****
  • Posts: 537
TListView doesn't have a sorted property
« Reply #4 on: April 22, 2005, 10:43:17 am »
Sorry, if I seem picky, but ...
In Delphi you have to implement any sorting yourself if you want to sort on anything else then the caption. When you set SortType to stText, it will automatically sort the items based on the value of the caption. If you want this sorted descending you have to write your own compare function. If you want the items to be sorted on anything else but the caption you have to set the ListView.SortType to stBoth or stData and again write your own compare function.
To get any action performed by clicking on a caption, you have to provide again your own code. At least this was the behaviour until Delhi 6 and by using the standard TListView.
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

Anonymous

  • Guest
TListView doesn't have a sorted property
« Reply #5 on: April 22, 2005, 06:45:11 pm »
Hello

This is my own implementation of "column click sort".
I am just a beginner, but it works, at least ascending.


(lv is the name of the listview)

procedure TForm1.lvColumnClick(Sender: TObject; Column: TListColumn);
var sl:TstringList; //used for sorting
    counter,
    SubItemsColumnCount,
    IndexOfCurrentColumn:integer;
begin
 sl := TStringList.Create;
 try
   IndexOfCurrentColumn:= column.index;
   
   if IndexOfCurrentColumn = 0 then begin
   
     for counter := 0  to lv.items.count -1 do begin
       sl. AddObject(lv.Items[counter].Caption,lv.items[counter]);
     end;
   end
   else begin
     for counter := 0  to lv.items.count -1 do begin
       SubItemsColumnCount:= lv.items[counter].subitems.Count;

       if (SubItemsColumnCount >= IndexOfCurrentColumn) then
          sl.AddObject(lv.items[counter].SubItems[IndexOfCurrentColumn-1],lv.items[counter])
       else
          sl.AddObject('',lv.items[counter]);
     end;
   end;

   sl.sort;

   for counter := 0  to lv.items.count -1 do begin
     lv.items[counter] := TListItem(sl. Objects[counter]);
   end;

 finally
   sl.free;
 end;

end

To make the sort descending, should be easy, I think.
As written before, this is the solution of a beginner.
Maybe the solution isn't "clean" or contains a lot of - for me - unknown bugs.

regards

Namrud

glober

  • Full Member
  • ***
  • Posts: 130
TListView doesn't have a sorted property
« Reply #6 on: April 23, 2005, 08:42:02 pm »
Quote from: "Namrud"
As written before, this is the solution of a beginner.
Maybe the solution isn't "clean" or contains a lot of - for me - unknown bugs.


Works quite well. I'm all for beginner stuff, if it does the job. :)

bonmario

  • Sr. Member
  • ****
  • Posts: 309
TListView doesn't have a sorted property
« Reply #7 on: March 07, 2007, 02:05:11 pm »
Quote from: "Anonymous"
Hello

This is my own implementation of "column click sort".
I am just a beginner, but it works, at least ascending.


It works fine, but if i have a item selected, for example the item number 2, aftter the sort, the selected item is the 2 too also if it was moved with the sort.

Example:

Listview before sort
a
c
d   <=== This is selected
b
e

ListView after sort:
a
b
c   <=== This is selected
d
e

There is some way to move the selection with the selected items?

Thanks, Mario

eric

  • Sr. Member
  • ****
  • Posts: 268
TListView doesn't have a sorted property
« Reply #8 on: March 07, 2007, 08:29:16 pm »
I don't see the difficulty here.

ListView1.Items.Sorted := True;

works just fine.

Eric

bonmario

  • Sr. Member
  • ****
  • Posts: 309
TListView doesn't have a sorted property
« Reply #9 on: March 09, 2007, 08:58:31 am »
Quote from: "eric"
I don't see the difficulty here.

ListView1.Items.Sorted := True;

works just fine.

Eric


Yes, but the code posted from user "Guest" works on Items and SubItems, but "ListView1.Items.Sorted := True;" works only on Items

 

TinyPortal © 2005-2018