- Home /
Help with sorting values from a class
Well, i need to do show some labels with bot's points in javascript, so I have a class Bot.
#pragma strict
class Bot extends MonoBehaviour
{
var name = "";
var animal = "";
var pointsScene1 : float = 0;
var pointsScene2 : float = 0;
var pointsScene3 : float = 0;
var totalPoints : float = 0;
function get Name() : String { return name; }
function set Name(value : String) { name = value; }
function get Animal() : String { return animal; }
function set Animal(value : String) { animal = value; }
function get PointsScene1() : float { return pointsScene1; }
function set PointsScene1(value : float) { pointsScene1 = value; }
function get PointsScene2() : float { return pointsScene2; }
function set PointsScene2(value : float) { pointsScene2 = value; }
function get PointsScene3() : float { return pointsScene3; }
function set PointsScene3(value : float) { pointsScene3 = value; }
function get TotalPoints() : float { return totalPoints; }
function set TotalPoints(value : float) { totalPoints = value; }
}
This class is populate with random values, until here is all perfect. But i need to "sort" this class to find out the the best bot with "TotalPoints" and complete the labels!
I assign they in Inspector using
var arr : Bot[];
Any ideas how can i do this?
I tryed something like this to find out
function Top8(arrTmp : Bot[]) {
var max = arrTmp[0].TotalPoints;
for (i = 1; i < arrTmp.Length; i++) {
if (arrTmp[i].TotalPoints > max) {
max = arrTmp[i].TotalPoints;
}
}
return max;
}
I called this function using
Top8(arr);
And I got the highest value TotalPoints, but I need to check from what bot is, and show it in GUI.Label to do a top 8 including 7 bots and player's points
Im accepting any sugestions!
Ins$$anonymous$$d of returning 'max' why not return 'arrTmp[i]' (the entire bot object)? You will have all the properties for that bot.
Answer by Eric5h5 · Apr 17, 2013 at 04:52 AM
Make a function to compare the totalPoints values, either inline:
var botsArray : Bot[];
function Start () {
System.Array.Sort (botsArray, function(a : Bot, b : Bot) a.totalPoints.CompareTo(b.totalPoints));
}
Or as a separate function:
var botsArray : Bot[];
function Start () {
System.Array.Sort (botsArray, CompareTotalPoints);
}
function CompareTotalPoints (a : Bot, b : Bot) : int {
return a.totalPoints.CompareTo(b.totalPoints);
}
Some observations: does Bot extend MonoBehaviour for a reason? You have the variables in the Bot class as public, so the getters/setters (which aren't doing anything) seem redundant.
Thanks Eric5h5, this compate function worked!
$$anonymous$$y Bot class extends $$anonymous$$onoBehavior because I need to assign the bot array in inspector, without this extend I cant do this.
Answer by The-IT664 · Apr 17, 2013 at 04:27 AM
I would suggest you take a look at a simple sorting algorithm like bubble sort (http://en.wikipedia.org/wiki/Bubble_sort). It's easy to understand and implement, and although it is quite inefficient as sorting algorithms go (n^2), your set is so small it won't make a meaningful difference.
Also, just a suggestion on your current implementation. Instead of storing the max TotalPoints, store and return the Bot instance itself and call Bot.TotalPoints to get the max value.
Edit: While you're at it, I highly recommend spending a bit of time learning about different sorting algorithms. They are vital in many different aspects of programming.
There's usually no real reason to make your own sorting algorithm when .NET/$$anonymous$$ono has sorting built in.