Sunday, 15 September 2013

c# - Using Comparison() comparison with the three arguments overload -



c# - Using Comparison<T>() comparison with the three arguments overload -

i have list<points>() , want sort custom comparer function.

i made:

class="lang-cs prettyprint-override">public int mycompare(point p1, point p2) { ... } // in main // ... points_.sort(mycompare); // ...

i works, right.

now want sort first element, thought do:

points_.sort(1, points_.count()-1, mycompare);

but overload wants argument icomparer.

how can solve this?

note point is not custom class, xna framework. don't want implement custom class : icomparer

as pointed out @dasblinkenlight .net 4.5+ there's ad-hoc method convert comparison<t> delegate icomparer<t>.

but if you're stuck lower version, can utilize class convert comparison<t> delegate icomparer<t>:

public class delegatecomparer<t> : icomparer<t> { private readonly comparison<t> compdelegate; public delegatecomparer(comparison<t> compdelegate) { if (compdelegate == null) throw new argumentnullexception("compdelegate"); this.compdelegate = compdelegate; } public int compare(t x, t y) { homecoming compdelegate(x, y); } }

usage example:

points_.sort(1, points_.count()-1, new delegatecomparer<point>(mycompare));

c# icomparer

No comments:

Post a Comment