Solved

TList.Sort

Posted on 1999-01-18
2
130 Views
Last Modified: 2010-04-06
Can someone explain how exactly the sort method of a TList is to be used?
Thanks.
0
Comment
Question by:franksymons
2 Comments
 
LVL 3

Accepted Solution

by:
philipleighs earned 50 total points
ID: 1362367
Hi,

Declare a function called MySortProc like this:

function MySortProc(Item1, Item2: Pointer): Integer;
  begin
    Result := 0;
    //Compare Item1 with Item2. Return -1, 0 or 1 for less, equal or greater
    //If the tlist object stores TYourClass objects, and you
    //want to sort by the Name property, then do this:
    if TYourClass(Item1).Name > TYourClass(Item2).Name then
      Result := 1
    else
    if TYourClass(Item1).Name < TYourClass(Item2).Name then
      Result := -1;
  end;


//Call this procedure to sort your list
procedure SortListOfYourClass(AList: TList);
  begin
    AList.Sort(MySortProc);
  end;


Cheers,
Phil.

PS: If you need more help, then describe what the list stores, and how you want to order the list items, and I'll give you some code to do it.
0
 

Author Comment

by:franksymons
ID: 1362368
Thanks, but do you have any idea why the sortprocedure can't be a method of a form or an object but has to be an ordinary procedure?
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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

17 Experts available now in Live!

Get 1:1 Help Now