Solved

Listview component sorting for D7

Posted on 2003-11-19
9
984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

719 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