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*.TotalPoints > max) {*

max = arrTmp*.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!

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.

I would suggest you take a look at a simple sorting algorithm like bubble sort (Bubble sort - Wikipedia). 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.