Implementing custom IComparer with string

This should do what you want: var example = new string[]{“c”, “a”, “d”, “b”}; var comparer = new CustomStringComparer(StringComparer.CurrentCulture); Array.Sort(example, comparer); … class CustomStringComparer : IComparer<string> { private readonly IComparer<string> _baseComparer; public CustomStringComparer(IComparer<string> baseComparer) { _baseComparer = baseComparer; } public int Compare(string x, string y) { if (_baseComparer.Compare(x, y) == 0) return 0; // “b” … Read more

Using IComparer for sorting

You need to implement the strongly type interface (MSDN). public class CoordinatesBasedComparer : IComparer<Point> { public int Compare(Point a, Point b) { if ((a.x == b.x) && (a.y == b.y)) return 0; if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y))) return -1; return 1; } } BTW, I think you use … Read more

Use own IComparer with Linq OrderBy

Your comparer looks wrong to me. You’re still just sorting in the default text ordering. Surely you want to be parsing the two numbers and sorting based on that: public int Compare(Object stringA, Object stringB) { string[] valueA = stringA.ToString().Split(“https://stackoverflow.com/”); string[] valueB = stringB.ToString().Split(“https://stackoverflow.com/”); if (valueA.Length != 2 || valueB.Length != 2) { stringA.ToString().CompareTo(stringB.ToString()); } … Read more