Solved

Listview component sorting for D7

Posted on 2003-11-19
9
961 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

19 Experts available now in Live!

Get 1:1 Help Now