Solved

TList's method 'sort'

Posted on 1998-09-14
5
222 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
Comment Utility
Edited text of question
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks JimBob.
I appriciate your good answer.

sakya

0
 
LVL 5

Expert Comment

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

Cheers,
JB
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

12 Experts available now in Live!

Get 1:1 Help Now