Created
September 11, 2015 18:37
-
-
Save jackfarrington/078e7af7bc82482aa634 to your computer and use it in GitHub Desktop.
C# Generic Reverse Comparer (IComparer<T>)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Gist | |
{ | |
public sealed class ReverseComparer<T> : IComparer<T> | |
{ | |
public static readonly ReverseComparer<T> Default = new ReverseComparer<T>(Comparer<T>.Default); | |
public static ReverseComparer<T> Reverse(IComparer<T> comparer) | |
{ | |
return new ReverseComparer<T>(comparer); | |
} | |
private readonly IComparer<T> comparer = Default; | |
private ReverseComparer(IComparer<T> comparer) | |
{ | |
this.comparer = comparer; | |
} | |
public int Compare(T x, T y) | |
{ | |
return comparer.Compare(y, x); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment