Solved

using TlistSortCompare to sort Tlist

Posted on 2001-09-01
10
1,382 Views
Last Modified: 2012-08-13
Hi, I know you can write a custom sorting function for Tlists but I can't work out how you actually define/call it.  I have defined Tjob as an object and one of its properties is StartTime :TdateTime.  I then have a list JobList:TJobList full of Tjob instances (or pointers to).  Please can you tell me how to call Joblist.sort() so that it calls a custom function to sort these all by starttime.  I know I have to create a TlistSortCompare function but just not sure how!  Thanks v much, P.
 
0
Comment
Question by:Pandora
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6448599
function JobListCompare(Item1, Item2: Pointer): Integer;
var

begin
     if TJobItem(Item1).StartTime>
end;
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 150 total points
ID: 6448600
Try this:


function JobListCompare(Item1, Item2: Pointer): Integer;
var
    Job1: TJobItem absolute Item1;
    Job2: TJobItem absolute Item2;
begin
    if Job1.StartTime<Job2.StartTime then
        Result:=1
    else if Job1.StartTime>Job2.StartTime then
        Result:=-1
    else
        Result:=0;
end;

[...]
    JobList.Sort(JobListCompare);
[...]
0
 
LVL 3

Expert Comment

by:Slavak
ID: 6449024
you can write it shorter:

function JobListCompare(Item1, Item2: Pointer): Integer;
var
   Job1: TJobItem absolute Item1;
   Job2: TJobItem absolute Item2;
begin
 Result := Job2.StartTime - Job1.StartTime;
end;



0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 6449038
Slavak, TDateTime is not an integer. Thus your version will fail. However, the following could be used:

function JobListCompare(Item1, Item2: Pointer): Integer;
var
    Job1: TJob absolute Item1;
    Job2: TJob absolute Item2;
    Temp: TDateTime;
begin
    Temp:=Job2.StartTime-Job1.StartTime;
    Result:=Ord(Temp>0)-Ord(Temp<0);
end;
0
 
LVL 3

Expert Comment

by:rondi
ID: 6450248
I thought the rules were
Item1 < Item2 -> -1
Item1 > Item2 -> 1
Item1 = Item2 -> 0

So shouldn't it be

   if Job1.StartTime < Job2.StartTime then
       Result := -1
   else if Job1.StartTime > Job2.StartTime then
       Result := 1
   else
       Result := 0;

???

which could be re-written as:

   Result := -1 + (2 * (Job1.StartTime > Job2.StartTime)) + (Job1.StartTime = Job2.StartTime);


rondi
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6450268
rondi, it depends whether the newest or the oldest dates should be listed first. However, the basic idea should be visible pretty well. BTW, you forgot to add "Ord()" around your boolean equations, and if you want to do two comparisons why not do it like this:

  Result:=Ord(Job1.StartTime>Job2.StartTime)-Ord(Job1.StartTime<Job2.StartTime);

If StartTime is a property which is not a field (e.g. involves a function call), this will be less efficient than storing the result in a temp variable. Since comparisons are called quite often when sorting, I think that speed could become an issue.
0
 
LVL 1

Author Comment

by:Pandora
ID: 6450368
Call me old fashioned but I'm gonna go for the long hand version!  Thanks v much for all your comments, I would never have got the 'absolute' part! Best wishes, P.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6450665
Pandora, the "absolute" part is not an absolute neccesity but rater to make things easier. You could also use typecasts to cast the pointers to your TJob classes, but the result would be the same.
0
 
LVL 1

Author Comment

by:Pandora
ID: 6451625
Gotcha - thanks v much! P :)
0
 
LVL 3

Expert Comment

by:rondi
ID: 6452418
Avon: I stand corrected :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 64
Can Viruses spread while transferring Binary data with Winsock 2 77
Making delphi communicate with a c# service 16 103
Magic Software info 18 131
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

810 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