Solved

TList's method 'sort'

Posted on 1998-09-14
5
226 Views
Last Modified: 2010-04-06
Hello.

Please teach me how to use the TList's method 'sort'.
I want to sort an array of records.

Thank you.

sakya
 
0
Comment
Question by:sakya
  • 3
  • 2
5 Comments
 

Author Comment

by:sakya
ID: 1339731
Edited text of question
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1339732
Hi

Set up a function CmpProc (or something else) as follows:

function CmpProc(Item1, Item2: Pointer): Integer;
begin
  // Return 1 if Item1 > Item2.
  if (TMyRec(Item1).RecStr > TMyRec(Item2).RecStr) then
    Result := 1
  else
    Result := -1;
end;

(In my example I use TMyRec which looks like this:
type
  TMyRec = record
    RecStr: string;
  end;
)

Then call "MyList.Sort(CmpProc);"

Cheers,
JB

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339733
Change CmpProc to the following:

function CmpProc(Item1, Item2: Pointer): Integer;
  if (TMyRec(Item1^).RecStr = TMyRec(Item2^).RecStr) then
    Result := 0
  else if (TMyRec(Item1^).RecStr > TMyRec(Item2^).RecStr) then
    Result := 1
  else
    Result := -1;
end;

JB
0
 

Author Comment

by:sakya
ID: 1339734
Thanks JimBob.
I appriciate your good answer.

sakya

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339735
You're welcome.  If there's any more help I can give, just ask.

Cheers,
JB
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

792 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