Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:22:44+00:00 2026-06-13T21:22:44+00:00

I’m kinda a Delphi-newbie and I don’t get how the Sort method of a

  • 0

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 the following:

 type
   TMyRecord = record
     str1: string;
     str2: string;
     intVal: integer;
   end;

And a generic list of such records:

TListMyRecord = TList<TMyRecord>;

Have tried to find a code-example in the help files and found this one:

MyList.Sort(@CompareNames);

Which I can’t use, since it uses classes. So I tried to write my own compare function with a little different parameters:

function CompareIntVal(i1, i2: TMyRecord): Integer;
begin
  Result := i1.intVal - i2.intVal;
end;

But the compiler always throws a ‘not enough parameters’ – error when I call it with open.Sort(CompareIntVal);, which seems obvious; so I tried to stay closer to the help file:

function SortKB(Item1, Item2: Pointer): Integer;
begin
  Result:=PMyRecord(Item1)^.intVal - PMyRecord(Item2)^.intVal;
end;

with PMyRecord as PMyRecord = ^TMyRecord;

I have tried different ways of calling a function, always getting some error…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:22:45+00:00Added an answer on June 13, 2026 at 9:22 pm

    The Sort overload you should be using is this one:

    procedure Sort(const AComparer: IComparer<TMyRecord>);
    

    Now, you can create an IComparer<TMyRecord> by calling TComparer<TMyRecord>.Construct. Like this:

    var
      Comparison: TComparison<TMyRecord>;
    ....
    Comparison := 
      function(const Left, Right: TMyRecord): Integer
      begin
        Result := Left.intVal-Right.intVal;
      end;
    List.Sort(TComparer<TMyRecord>.Construct(Comparison));
    

    I’ve written the Comparison function as an anonymous method, but you could also use a plain old style non-OOP function, or a method of an object.

    One potential problem with your comparison function is that you may suffer from integer overflow. So you could instead use the default integer comparer.

    Comparison := 
      function(const Left, Right: TMyRecord): Integer
      begin
        Result := TComparer<Integer>.Default.Compare(Left.intVal, Right.intVal);
      end;
    

    It might be expensive to call TComparer<Integer>.Default repeatedly so you could store it away in a global variable:

    var
      IntegerComparer: IComparer<Integer>;
    ....
    initialization
      IntegerComparer := TComparer<Integer>.Default;
    

    Another option to consider is to pass in the comparer when you create the list. If you only ever sort the list using this ordering then that’s more convenient.

    List := TList<TMyRecord>.Create(TComparer<TMyRecord>.Construct(Comparison));
    

    And then you can sort the list with

    List.Sort;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.