Solved

Listview component sorting for D7

Posted on 2003-11-19
9
963 Views
Last Modified: 2007-12-19
I am looking for a listview component for Delphi 7 that would allow me to sort ascending and descending when you click on the column header. It must also show a sort icon to indicate the relevant sort. Or if someone have code to do this with the standard listview component for Delphi 7, it would also be great! Thanx in advance!
0
Comment
Question by:skynergy
9 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 25 total points
ID: 9779856
Here's how I do it using standard TListview sorting methods. Sorry, but it does not include the  header graphic.

This sorts integer in column 3 and everything else uses an alphasort.

{***********************************************************
   Listview Integer column sort courtesy of "Granger"
   Originally written to handle any type data, modified to
   work with Integer data only.
   Don't forget stdcall!!!
***********************************************************}
var
  FSortColumn: Integer;
  FAscending : Boolean;

function LVIntegerSort(lParam1, lParam2: Integer; lParamSort: Integer): Integer; stdcall;

  function LVItemValue(const Item: TListItem; Col: Integer): Integer;
  begin
    if Item = nil then
      Result := -1
    else
      Result := StrToInt(Item.SubItems[Col - 1]);
  end;

var
  vData1: Integer;
  vData2: Integer;
begin
  try
    vData1 := LVItemValue(TListItem(lParam1), 3);
    vData2 := LVItemValue(TListItem(lParam2), 3);

    if vData1 > vData2 then
      Result := 1
    else if vData1 < vData2 then
      Result := -1
    else begin
      Result := 0;
    end;
    if not FAscending then
      Result := -Result;
  except
    Result := 0;
  end;
end;

procedure TfmHotmailForm.MessageViewCompare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
begin
  if FAscending then
  begin
    case FSortColumn of
    0:   Compare := CompareText(Item1.Caption, Item2.Caption);
    else Compare := CompareText(Item1.SubItems[FSortColumn-1],
                                Item2.SubItems[FSortColumn-1]);
    end;
  end
  else
  begin
    case FSortColumn of
    0:   Compare := CompareText(Item2.Caption, Item1.Caption);
    else Compare := CompareText(Item2.SubItems[FSortColumn-1],
                                Item1.SubItems[FSortColumn-1]);
    end;
  end;
end;

procedure TfmHotmailForm.MessageViewColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  FAscending := not FAscending;
  FSortColumn := Column.Index;
  case FSortColumn of
  3: // Message Size column needs special sorting.
    TListView(Sender).CustomSort(LVIntegerSort, Integer(TListView(Sender)));
  else
    TListView(Sender).AlphaSort;
  end;
end;
0
 

Author Comment

by:skynergy
ID: 9795965
Thank you for your answer. I need a sort for non integer data actually. Any comments?
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 25 total points
ID: 9796587
If I may suggest something... Why not turn the listview into a virtual listview, using the listview events to fill the data on demand. A virtual listiew is especially useful with very large amounts of records.
Now, if you use a virtual listview, you could e.g. store the data in a TList and sort that list whenever it needs to be sorted.

Then again, Eddie gave a valid answer. All you need to do in the LVIntegerSort routine is compare two values from the listview. The function provides you two listview nodes and you just have to compare them. Eddie converted them to integers for an integer comparison but it works just as well with other datatypes if you convert to the proper data type.
Eddies solution won't work with a virtual listview, though.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:skynergy
ID: 9796878
Thanx for your answer. Luckily I don't have large amounts of data that I need to display in the listview. How would I display the sort arrow icon in the title column though?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9799653
See this post for placing sort images in the header:

http://www.delphipages.com/tips/thread.cfm?ID=74
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9799664
Apparently there may be a problem with the images disappearing when the columns are resized.
I will see if I can find an answer for that one, too.
0
 

Expert Comment

by:hanson04
ID: 9843747
Try out this component
  1)   http://www.sharemation.com/apkoomeh/Mypage/IListBox.zip
Enjoy!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 198
proper way to parse text with delphi 7 99
Strange behavior when a form is closed 6 49
LAN or WAN ? 11 81
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now