- Home /
How do I Sort a List of Classes by a property?
I have a List of my Class
var Events = List.<Evnt>();
where the Class Evnt has a property enentTime
public class Evnt{
//properties
var eventNo :int;
var eventTime :float;
//Constructor
public function Evnt(evn:int,evt:float){
this.eventNo = evn;this.eventTime = evt;
}
}
I want to sort this List by eventTime. If I call Events.Sort() I get an error - ArgumentException: does not implement right interface. How can I sort a List in this manor? Thank You.
Answer by Dreamblur · Jul 17, 2011 at 09:19 PM
Yes. I can sort a list of ints using simply Sort(). I need the right code to sort by eventTime - Events.Sort(); Events.Sort(eventTime); Events.Sort(Evnt.eventTime); all give errors. What do I need to put in the parenthises?
You have to write your own comparison method that returns whether an object is "equal", "greater than" or "less than". Check the examples found on those pages.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
javascript, default object comparator 1 Answer
Lists and Structs instead of Arrays? 4 Answers
class array removeAt 1 Answer
How to store children of a class inside that class 0 Answers