How do I sort a generic list using a custom comparer?

后端 未结 4 1696
谎友^
谎友^ 2020-11-30 00:08

I\'m kinda a Delphi-newbie and I don\'t get how the Sort method of a TList of Records is called in order to sort the records by ascending integer value. I have a record like

4条回答
  •  一生所求
    2020-11-30 00:20

    I found a much simpler modified sort function to alphabetize a TList of records or nonstandard list of items.

    Example

    PList = ^TContact;
        TContact = record             //Record for database of user contact records
          firstname1 : string[20];
          lastname1 : string[20];
           phonemobile : Integer;       //Fields in the database for contact info
          phonehome : Integer;
          street1 : string;
          street2 : string;
    
     type
        TListSortCompare = function (Item1,
                                    Item2: TContact): Integer;
    var
      Form1: TForm1;
      Contact : PList;         //declare record database for contacts
      arecord : TContact;
      Contacts : TList;   //List for the Array of Contacts
    
    function CompareNames(i1, i2: TContact): Integer;
    begin
       Result := CompareText(i1.lastname1, i2.lastname1) ;
    end;
    

    and the function to call to sort your list

    Contacts.Sort(@CompareNames);
    

提交回复
热议问题