Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sort on a field in an inherited TObjectList

Posted on 2010-11-18
8
Medium Priority
?
757 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: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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

877 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