Solved

Listview component sorting for D7

Posted on 2003-11-19
9
979 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi IDE crash without error message ... 7 121
Firemonkey webbrowser scrollbars ? 1 57
CheckListBox usage 3 82
Firemonkey how to swip panel with finger tap 1 23
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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