I have a List
where T
is my Event
type which has a field time
of type long
. This list is populated from
Try
events.OrderBy (e => e.Time == 0).ThenBy (e => e.Time);
You could use LINQ:
resp.OrderBy(i => i.time == 0 ? int.MaxValue : i.time);
Your custom IComparer is incorrect. With correct logic, it should work just fine. The problem is that if the left value is zero, any value will be equal to it. This means that 0 == 3
is true, and 3 > 0
is true. In fact, 0 > 3
and 3 < 0
should be true
You should do something like this instead:
if (x.time == y.time) return 0;
if (x.time == 0) return 1;
if (y.time == 0) return -1;
return x.time.CompareTo(y.time);
You should do something like this:
if(x.time==y.time) return 0;
if(x.time==0) return 1;
return x.time - y.time;
The point here is that 0 is larger than any other time, so it'll be placed at the end of the list.
Try this one (you have to adjust it for your needs):
class Comparer : IComparer<int>
{
public int Compare(int x, int y)
{
if (x == y) return 0;
else if (x == 0) return 1;
else if (y == 0) return -1;
else if (x < y) return -1;
else if (x > y) return 1;
}
}