Hello, everyone!
My issue is, when I’m trying to Sort a List of RaycastHit2D’s with CompareTo method it doesn’t work.
Here’s the code part:
RaycastHit2D[] hits = Physics2D.BoxCastAll (caststart, playersize, 0f, castdir, c5_maxrange);
List<RaycastHit2D> hitList = new List<RaycastHit2D> ();
hitList.AddRange (hits);
Debug.Log ("Objects withOUT sorting");
foreach (RaycastHit2D hit in hitList){
Debug.Log (hit.collider.name + " " + hit.collider.bounds.max.x);
}
// return;
hitList.Sort(delegate(RaycastHit2D x, RaycastHit2D y) {
return(x.collider.bounds.max.x.CompareTo(y.collider.bounds.max.x));
});
Debug.Log ("Sorted Objects");
foreach (RaycastHit2D hit in hits){
Debug.Log (hit.collider.name + " " + hit.collider.bounds.max.x);
}
And, unfortunately for me, the result before and after sorting doesn’t change. The reason I need this sorting is that BoxCastAll catches upper colliders first. then the lower ones, and when I start to check members in the list in reverse order I would like to check upper collider’s first. But basically, I’m trying to understand why Sort doesn’t actually sort. Thank you for the answer in advance.