Board index » delphi » TLIST - MyList.Sort(Compare: TListSortcompare);

TLIST - MyList.Sort(Compare: TListSortcompare);

 Dear Sirs,

I have appended my Listing below.   I looked as to how TList.Sort (Compare:
TListSortcompare) should be used and how parameters should be passed.  I
found some description on the ONLINE Help but with out example.

With out examples I am still confused how and what parameters should be
passed in my following listing, I just could not find the right answer.

I would be most grateful if you could give any help.

Faithfully,
Rakam
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure TManageLogInIDForm.SortLogInID;
type

   PMyList = ^PwdRecord;

   PwdRecord = record

      cLogInID: String[4];

      cGroup: String[8];

      cPwd : String[8];

      PwdDate: TDateTime;

      cUser: String[4];

   end;

   var i:Integer;

      MyList: TList;

      ARecord: PMyList;

      //UserLogInIDFile: Array of PwdRecord;

     //This only example here.

begin

   i := 0;

   MyList := TList.Create;

   try

      for i := 0 to High(UserLogInIDFile) do

      begin

         New(ARecord);

         ARecord^.cLogInID := UserLogInIDFile[i].cLogInID;

         ARecord^.cGroup := UserLogInIDFile[i].cGroup;

         ARecord^.cPwd   := UserLogInIDFile[i].cPwd;

         ARecord^.PwdDate := UserLogInIDFile[i].PwdDate;

         ARecord^.cUser   := UserLogInIDFile[i].cUser;

         MyList.Add(ARecord);

      end;

      //MyList.Sort(Compare: TListSortcompare); // This is what

                                    //the helpline says but without example.

      MyList.Sort(?? What parameters?)

   for i := 0 to (MyList.Count - 1) do

      begin

         ARecord := MyList.Items[i];

         Dispose(ARecord);

      end;

   Finally

      MyList.Free;

   end;

end;

 

Re:TLIST - MyList.Sort(Compare: TListSortcompare);


Hallo,

here's a example, how TList.Sort can be used:

function YourListSortCompare(Item1, Item2: Pointer): Integer;
var
    It1, It2: PMyList;
begin
    // it's wise, because you don't need type-casts in comparisons later
    It1 := Item1;
    It2 := Item2;
    // now you implement the heart of getting your sort-order
    if It1^.YourField > It2^.YourField then
       Result := 1
    else if It1^.YourField < It2^.YourField then
       Result := -1
    else
       Result := 0;
end;

Now you can use it:

MyList.Sort(YourListSortCompare);

Regards
Stephan Schneider

Re:TLIST - MyList.Sort(Compare: TListSortcompare);


Dear Stephan,

Further to my thanking letter to you, I wish your codes and my question were
included on Delphi Online Help.  It would be greate help to all Delphi
programmers.

rakam

Re:TLIST - MyList.Sort(Compare: TListSortcompare);


Dear Stephan,

Thank you very much for you kind help.  Your valuable code worked exactly as
I wanted, and my problem of indexing and sorting is solved.

None the less how this code works is still beyond my understanding.

I simply pasted your code and renamed Vars as per my requirements, and it
was magic, it worked!!

Thank you very much once again.

Sincerely,
Rakam

Quote
"Stephan Schneider" <stephan.schnei...@web.de> wrote in message

news:3b0b519b_1@dnews...
Quote
> Hallo,

> here's a example, how TList.Sort can be used:

> function YourListSortCompare(Item1, Item2: Pointer): Integer;
> var
>     It1, It2: PMyList;
> begin
>     // it's wise, because you don't need type-casts in comparisons later
>     It1 := Item1;
>     It2 := Item2;
>     // now you implement the heart of getting your sort-order
>     if It1^.YourField > It2^.YourField then
>        Result := 1
>     else if It1^.YourField < It2^.YourField then
>        Result := -1
>     else
>        Result := 0;
> end;

> Now you can use it:

> MyList.Sort(YourListSortCompare);

> Regards
> Stephan Schneider

Re:TLIST - MyList.Sort(Compare: TListSortcompare);


Dear Stephan,

How do you look into the contents of  TList.Items.  I was unable to see the
contents unless I copied into another Multi-dimensional Array.

Sincerely,
Rakam

Other Threads