Solved

TList's method 'sort'

Posted on 1998-09-14
5
228 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
[X]
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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
Delphi 2 77
Base1 Encode/Decode 3 86
Firemonkey DbLookupComboBox equivalent ? 2 67
DBGrid or StringGrid ? 6 120
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…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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