My problem is that I always want to arrange the collection of objects in a certain way.
For instance:
class foo{ public string name {get;set;} public DateTime date {get;set;} public int counter {get;set;} }
...
IEnumerable<foo> dosomething(foo[] bar){ return bar.OrderBy(a=>a.name).ThenBy(a=>a.date).ThenBy(a=>a.counter); }
The question I have is a rather lengthy binding to the sort order all the time. The optimal solution seems to just create a class that implements IComparer<foo> , which I can do:
IEnumerable<foo> dosomething(foo[] bar){ return bar.OrderBy(a=>a, new fooIComparer()) }
.
The problem is that the implemented order method is as follows
...
public int Compare(foo x, foo y){ }
This means that it is compared on a very competent basis.
The current implementation (which is likely to work though im writing pseudocode)
public int Compare(foo x, foo y){ if (x==y) return 0; var order = new []{x,y}.OrderBy(a=>a.name).ThenBy(a=>a.date).ThenBy(a=>a.counter); return (order[0] == x) ? -1 : -1;
This is not entirely effective, can another suggest a faster solution? Ideally, without the Compare (x, y) method?
source share