Custom Sorting of List

前端 未结 5 917
清酒与你
清酒与你 2021-01-21 03:11

I have a List where T is my Event type which has a field time of type long. This list is populated from

5条回答
  •  遇见更好的自我
    2021-01-21 03:31

    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);
    

提交回复
热议问题