Tuesday, July 28, 2009

I want to sort a List in C# but how do I break ties?

This is my line of code that works perfectly at the moment:





myConsiderList.Sort(delegate(Dot d1, Dot d2) { return d1.getRight().getX().CompareTo(d2.getRig... });





However, let's say there is a tie between d1 and d2. I don't want to break the tie arbitrarily. If they are equal, I'd like to check this:





d1.getLeft().getX().CompareTo(d2.getLe...





If they tie, then it doesn't matter who I pick. Is there an easy way for me to include this into my code? Thanks.

I want to sort a List in C# but how do I break ties?
Your return would be different, more like:





if(d1.getRight().getX()==d2.getRight()...


return the left comparisons you wrote about


} else{


return the original thing you had


}





Should work just fine.


No comments:

Post a Comment