Jump to content
Sign in to follow this  
caymon

How to sort only a part of a list

Recommended Posts

I have a list of objects, declared like TMyList = class(TObjectList<T>) with say  1000 elements.

The list is sorted according to a certain comparer.

 

Then, how do I sort again, but only the first 100 elements of that list, according to another comparer.

 

The construction MyList.Sort(...) doesn't allow to specify a low and a high bound of the sort.

Share this post


Link to post

You didn't mention the Delphi version, but since Delphi 11 you can use the appropriate Sort overload taking a comparer, start index and count.

procedure Sort; overload;
procedure Sort(const AComparer: IComparer<T>); overload;
procedure Sort(const AComparer: IComparer<T>; Index, Count: Integer); overload;

 

  • Like 2
  • Thanks 1

Share this post


Link to post

Thank you for the info about Delphi 11.

Indeed, I have that version but the program that needs the change (600K lines) was compiled using 10.4.

However, I've checked that it also compiles (and seems to run) properly using 11, maybe it is time to switch!

Share this post


Link to post

Using 10.4 you can do this with the following code:

  TArray.Sort<yourtype>(list.PList^, list.Comparer, index, count);

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×