- Home /
javascript, default object comparator
i have a simple buff class and a generic list full of buffs. and i need to sort them but the reference for the list's sort function says:
"Sorts the elements in the entire List using the default comparer."
i need to override the default comparer in my buff class so they are sorted how i intended, but i'm not sure what function's name is, so i can't start to override it.
this is the class:
#pragma strict
class Buff
{
var on: boolean;
var name: String;
var effect1: String;
var effect2: String;
var power1: float;
var power2: float;
var duration: float;
function Buff(newName:String, e1: String, e2: String, p1: float, p2: float, dur:float)
{
on = true;
name = newName;
effect1 = e1;
effect2 = e2;
power1 = p1;
power2 = p2;
duration = dur;
}
}
It's really annoying to try and use the more complex .net libraries in Unity javascript. If no one picks this question up, I'll give it a go.
Answer by ArkaneX · Nov 27, 2013 at 06:43 PM
One way to achieve what you want, is to change your class so that it implements generic IComparable
interface, and uses CompareTo
function:
class Buff implements System.IComparable.<Buff>
{
// all your class stuff
function CompareTo(other : Buff)
{
if(other == null)
{
return 1;
}
return this.power1.CompareTo(other.power1); // or any other suitable code to properly compare required values
}
}
sorry i was away from my pc yesterday, thanks for the answer. it all works perfect now :)
Your answer
Follow this Question
Related Questions
How do I Sort a List of Classes by a property? 2 Answers
A node in a childnode? 1 Answer
Sorting a list by distance to an object? 1 Answer
Collisions and Lists 1 Answer