Solved

Sort on a field in an inherited TObjectList

Posted on 2010-11-18
8
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34169998
:) I have to try and stay a step ahead otherwise you give me a good whipping
0
 
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:
Ephraim Wangoya 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month5 days, 3 hours left to enroll

636 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