Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sort on a field in an inherited TObjectList

Posted on 2010-11-18
8
Medium Priority
?
749 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

721 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