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
Solved

Listview component sorting for D7

Posted on 2003-11-19
9
975 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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