Solved

Sort on a field in an inherited TObjectList

Posted on 2010-11-18
8
719 Views
Last Modified: 2012-05-10
When sorting a generic TObjectList inherited from another I get a compilation error
“There is no overload version of Sort that can be called with these arguments”

To explain with an example -

TPerson = class
  FFirstName : string;
  FLastName : string;
end;

TPersonList = class(TObjectList<TPerson>)
 end;

I can sort TPerson on a name field  using

procedure TForm1.Button3Click(Sender: TObject);
begin
  PersonsList.Sort(TComparer<TPerson>.Construct(
   function(const Item1,Item2:TPerson): Integer
   begin
      Result := CompareText(Item1.LastName, Item2.LastName);
   end));
end;

Now, if we have two inherited classes

 TChild = class(TPerson)
 private
  fAge: integer;
and
TChildList = class(TPersonList)
 end;

I get a compilation error (“There is no overload version of Sort that can be called with these arguments”) with

procedure TForm1.Button4Click(Sender: TObject);
begin
  ChildList.Sort(TComparer<TChild>.Construct(
   function(const Item1,Item2:TChild): Integer
   begin
      Result := Item1.Age - Item2.Age;
   end));
end;

Any suggestions on how to rectify this.

0
Comment
Question by:Dennis_Treacy
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 34169947
Thats because ChildList is a list of TPerson

You can declare it as

  TChildList = class(TObjectList<TChild>)
  end;
0
 
LVL 25

Expert Comment

by:epasquier
ID: 34169966
declare
 
TChildList = class(TObjectList<TChild>) end;

Open in new window

otherwise you inherit the parameter types of all functions from TPersonList, so TPerson instead of the TChild that you want.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 34169974
Ok ewangoya, you got me ;o)
are you Speedy Gonzales cousin ?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34169998
:) I have to try and stay a step ahead otherwise you give me a good whipping
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:epasquier
ID: 34170073
I know, take that as an honorific gesture. It's because you annoy me, as JimyX, and aflarin before, being on all questions around while at the same time I have had a lot of work and couldn't quite respond as quickly and often as before...
I have the impression it will take forever to get those 20k points that will let me enter top 5... I hoped to achieve it in less than a year, but that day has passed and I still see no end of it.

So because of that frustration, I make you pay whenever you don't give your best

That is all the more futile from me that I annoyed Geert_Gruwez pretty much the same way a year ago.

Sorry again for being a bit caught in that game ;o)
0
 

Author Comment

by:Dennis_Treacy
ID: 34170124
ewangoya:

Thanks, but I actually want to sort TChildList without foregoing access to the methods in TPersonList, that's why TChildList is inherited from TPersonList. I made up this small example for the purposes of this question, but my real life case has a bunch of object lists inherited from some base object lists so I can share methods and this is where I want to use the solution

Is it possible to sort TChildList without loosing access tp methods in TPersonList - I tried, without luck

  TChildList(PersonsList).Sort(TComparer<TChild>.Construct(
   function(const Item1,Item2:TChild): Integer
   begin
      Result := Item1.Age - Item2.Age;
   end));

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34170259
You can do a type cast, this will work but it is not clean

 ChildList.Sort(TComparer<TPerson>.Construct(
   function(const Item1, Item2: TPerson): Integer
   begin
      Result := TChild(Item1).Age - TChild(Item2).Age;
   end));
0
 

Author Closing Comment

by:Dennis_Treacy
ID: 34170306
Great! Thanks very much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now