c# - Sort List with duplicate keys by custom comparer -
i have list
inputcoll
of type myvalue
many duplicate keys:
myvalue1.id=100; myvalue2.id=100; ...etc
and have custom comparer compare 2 myvalue
elements id
:
inputcoll.sort(myvaluecomparer);
what doing wrong?
comparer:
public class myvaluecomparerbyid : icomparer<myvalue> { public int compare(myvalue x, myvalue y) { if (x.id == y.id) return 0; else if (x.id > y.id) return -1; else if (x.id < y.id) return 1; return 0; } }
unless equality comparer not implemented badly, solution should work.
but suggest easier approach, using linq:
inputcol = inputcol.orderby(o => o.id).tolist();
Comments
Post a Comment