- Home /
Question by
felipopulus · Jan 26, 2015 at 07:19 AM ·
ordercomparesort
IComparable returning unpredictable Sort() results
I think IComparable is returning unpredictable results... but i'm sure someone will point out something that i'm overlooking. The following is a boo script
import UnityEngine
import System
public class PointSortedByDistance(IComparable[of PointSortedByDistance]):
public point as Vector3
public distance as double
public def constructor(newPoint as Vector3, newDistance as double):
point = newPoint
distance = newDistance
//This method is required by the IComparable
public def CompareTo(other as PointSortedByDistance) as int:
if other is null:
return 1
// return distance - other.distance
return other.distance.CompareTo(distance)
Then I run this:
import UnityEngine
class TestPointSortedByDistance (MonoBehaviour):
def Start ():
sorted_points_list as List[of PointSortedByDistance] = List[of PointSortedByDistance]()
sorted_points_list.Add(PointSortedByDistance(Vector3.zero, 3.0 cast double))
sorted_points_list.Add(PointSortedByDistance(Vector3.up, 3.1 cast double))
sorted_points_list.Add(PointSortedByDistance(Vector3.down, 3.8 cast double))
sorted_points_list.Add(PointSortedByDistance(Vector3.right, 2.8 cast double))
sorted_points_list.Sort()
test = []
for a as PointSortedByDistance in sorted_points_list:
Debug.Log(a.point + ": " + a.distance)
test.Add(a.distance)
Debug.Log(test.Sort())
sorted_points_list.Sort() is sorted out of order. However, the "test" list is sorted correctly. Any Ideas? I'm very curios to find out what i've done wrong.
Comment