Solved

Sort on a field in an inherited TObjectList

Posted on 2010-11-18
8
722 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy file in dll not working but working on exe ! 18 92
Delphi TcxGrid group footer summary 3 257
Convert GUI app into console app for Win32 Env 5 98
Base1 Encode/Decode 3 77
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

11 Experts available now in Live!

Get 1:1 Help Now