Board index » delphi » How do I use "procedure TList.SortSort(Compare: TListSortCompare);"

How do I use "procedure TList.SortSort(Compare: TListSortCompare);"

I set this up with a function, but the pointers never pointed to anything I
could recognize.

type
    PSearchRecStruct = ^TSearchRecStruct;

procedure TJDIliPen.SortSearchRecs;
begin
    FSearchRecsList.Sort(CompareSearchRecs);
end;

function CompareSearchRecs(Item1, Item2: Pointer): Integer;
var
 TPRec: TJdIliTPRec;
 SerialNo1, SerialNo2: string;
 SearchRecStruct: TSearchRecStruct;
begin
 SearchRecStruct := (PSearchRecStruct(Item1))^;
 TPRec := ConvertRawRecToTPRec(SearchRecStruct.RawRec);
 SerialNo1 := TPRec.SerialNo;
 SearchRecStruct := (PSearchRecStruct(Item2))^;
 TPRec := ConvertRawRecToTPRec(SearchRecStruct.RawRec);
 SerialNo2 := TPRec.SerialNo;
 Result := CompareStr(SerialNo1, SerialNo2);
end;

If anyone has a complete example of how to use the Sort procedure please
email it to me.
Please and Thank you.
Jim
j...@ilitech.com

 

Re:How do I use "procedure TList.SortSort(Compare: TListSortCompare);"


It would be VERY NICE to know your logic in filling up the TList. Then I could
better tell you if I see a problem. What you might want to do is to DISPLAY or
writeln() the values of Item1 & Item2 upon entry to the compare routine, and
further do a writeln on the SEARCHRECSTRUCT fields after you do the assignment
to see if the values displayed are correct.

Davie

Quote
Jim Donachiue wrote:
> I set this up with a function, but the pointers never pointed to anything I
> could recognize.

> type
>     PSearchRecStruct = ^TSearchRecStruct;

> procedure TJDIliPen.SortSearchRecs;
> begin
>     FSearchRecsList.Sort(CompareSearchRecs);
> end;

> function CompareSearchRecs(Item1, Item2: Pointer): Integer;
> var
>  TPRec: TJdIliTPRec;
>  SerialNo1, SerialNo2: string;
>  SearchRecStruct: TSearchRecStruct;
> begin
>  SearchRecStruct := (PSearchRecStruct(Item1))^;
>  TPRec := ConvertRawRecToTPRec(SearchRecStruct.RawRec);
>  SerialNo1 := TPRec.SerialNo;
>  SearchRecStruct := (PSearchRecStruct(Item2))^;
>  TPRec := ConvertRawRecToTPRec(SearchRecStruct.RawRec);
>  SerialNo2 := TPRec.SerialNo;
>  Result := CompareStr(SerialNo1, SerialNo2);
> end;

> If anyone has a complete example of how to use the Sort procedure please
> email it to me.
> Please and Thank you.
> Jim
> j...@ilitech.com

Re:How do I use "procedure TList.SortSort(Compare: TListSortCompare);"


Your compare function depends on what your store in the list. As this is not
clear in your example I can't see what goes wrong.
If you look at the example below you will see how we typecast the pointers
to the excact same thing that we have add to the list.
This is offcourse very important, so try check this first. Also note that
the example will not survive when you leave procedure test, as the records
that we have entered goes out of scope at this point.
Another thing, when you have multiple criterias, split each criteria into a
unique CompareFunction. Fx. ByName, ByLastName etc.

Regards

type
  pSomeRec = ^TSomeRec;
  TSomeRec = record
    Name: String;
    LastName: String;
  end;

function ByLastName( A, B: Pointer ): Integer;
begin
   Result := CompareStr( pSomeRec(A)^.LastName, pSomeRec(B)^.LastName );
end;

function ByName( A, B: Pointer ): Integer;
begin
   Result := CompareStr( pSomeRec(A)^.Name, pSomeRec(B)^.Name );
end;

function By_Name_LastName( A, B: Pointer ): Integer;
begin
  Result := ByName( A, B );
  if Result = 0 then Result := ByLastName( A, B );
end;

procedure test;
var
  sr1, sr2, sr3 : TSomeRec;
  list : TList;
begin
  list := TList.Create;
  sr1.Name := 'Z';
  sr2.Name := 'A';
  sr2.Name := 'B';
  list.add( @sr1 );
  list.add( @sr2 );
  list.add( @sr3 );
  list.Sort( By_Name_LastName );
  ....
  list.Free;
end;

Re:How do I use "procedure TList.SortSort(Compare: TListSortCompare);"


Thank you all for your help I will certainly try this again or use a
TStringList if I can't make it work.
Thank you and have a merry Xmas ana a Happy Y2k.

Jim

Other Threads