c# - Comparing for sorting in different ways -
i have class implements icomparable. works comparison static, i.e. it's same ordering achieves. method introduce comparison parameter, i.e. if have:
class poo : icomparable { public int { ... }; public int b { ... }; ... } ienumerable<foo> list = ...; list = list.sort(???);
i' d order list respect a or b depending on parameter passed sort @ question marks. what's efficient way that?
at moment, best method i've came declare couple of methods pass sort delegates.
private static int comparewrta(foo foo1, foo foo2) { ... } private static int comparewrtb(foo foo1, foo foo2) { ... } if(withrespecttoa) list = list.sort(comparewrta); else list = list.sort(comparewrtb);
but doesn't feel best way. criticism welcome.
if want simplify statement can write this:
list.sort((x,y) => withrespecttoa ? comparewrta(x,y) : comparewrtb(x,y));
btw, sort
method modifying list, doesn't return anything.so don't need assign list.
Comments
Post a Comment