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

  • SEARCH
  • Home
  • 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 8481609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:36:10+00:00 2026-06-10T19:36:10+00:00

I need to implement a binary search on TObjectList that uses a custom comparer,

  • 0

I need to implement a binary search on TObjectList that uses a custom comparer, I believe using TCustomComparer.

Goal: binary search returns instances in the list that conform to a particular property parameter.

For example:

TMyClass=class
    public
     Index:integer
end;

TMyObjectList=TObjectList<TMyClass>;

begin
  ...
    aMyClass.Index:=1;
    aMyObjectList.binarysearch(aMyClass, aMyClassRef) 
  ...
end;

Or simply:

 begin
   ...
     aMyObjectList.binarysearch(1, aMyClassRef) 
   ...
 end;

I want to loop and get back instances of TMyClass in the list that also have Index==1.

In C++, overloading the ‘==’ operator achieves this goal.

The new Delphi ‘help’ is rather sparse and scattered around making things hard to find, and I’m not that familiar with all the nuances of the new Delphi generics.

So – how do I do it in Delphi XE with the Generics.TObjectList?

(Using Delphi XE).

TIA

  • 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-10T19:36:11+00:00Added an answer on June 10, 2026 at 7:36 pm

    The help file is indeed a little limited here. I generally just read the source code to Generics.Defaults and Generics.Collections. Anyway, you need to provide an IComparer<TMyClass>. There’s lots of ways to do that. For example, using an anonymous function:

    var
      List: TObjectList<TMyClass>;
      Target: TMyClass;
      Index: Integer;
      Comparer: IComparer<TMyClass>;
      Comparison: TComparison<TMyClass>;
    ....    
    Comparison :=
      function(const Left, Right: TMyClass): Integer
      begin
        //Result := ??;//your comparison rule goes here
      end;
    Comparer := TComparer<TMyClass>.Construct(Comparison);
    List.BinarySearch(Target, Index, Comparer);
    

    If you don’t want to use an anonymous function you can implement Comparison some other way. For example a method of some object, or a class function, or even just a plain old fashioned non-OOP function. It just has to have the same signature as above.

    As always with comparison functions, return <0 if Left<Right, >0 if Left>Right and 0 if Left=Right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to implement a messaging scenario that consumes messages from throusands of destinations
I am using C to implement a packet stream that will be sent out
I need to pass some binary data between two android apps using Content Provider
I need to implement inter-process communication system for processes (in machine-wide) in Win32 using
I am writing a Generic Binary Search Tree. I need to compare two generic
I need to implement secure data transfer from client to server (binary data). I'm
I need to design a custom text view that displays logs that my application
I came across this problem,I have got an algorithm that I need to implement
I need to implement the following function. I'm open to using the best class,
I need to implement portable code, but I do not know how to deal

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.