Solved

TList's method 'sort'

Posted on 1998-09-14
5
225 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 90
Print Graphic and Text to Epson TM-T88v 12 259
Delphi 2 60
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 67
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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