First of all I have to specify that I'm working in Unity 5.3 and the new MonoDevelop doesn't allow me to debug. Unity just crashes :(
So I have a list of "goals" that I need to sort based on 3 criterias:
- first should be listed the "active" goals
- then ordered by difficulty level
- finally randomly for goals of the same level
here is a my code:
public class Goal {
public int ID;
public int Level;
public bool Active;
}
...
List<Goal> goals;
goals.Sort((a, b) => {
// first chooses the Active ones (if any)
var sort = b.Active.CompareTo(a.Active);
if (sort == 0) {
// then sort by level
sort = (a.Level).CompareTo(b.Level);
// if same level, randomize. Returns -1, 0 or 1
return sort == 0 ? UnityEngine.Random.Range(-1, 2) : sort;
} else {
return sort;
}
});
When I run this code sometimes I get one or more active goals after inactive ones, but I don't understand why.