IComparer for several values

Is it possible to sort a list using two values ​​in an object with iComparer?

I have a custom comparison class that is sorted based on value1. But what is the best way to get sorting by value1 and value2?

Will the list be sorted by value2 and then value1?

+4
source share
3 answers

Your IComparer class should handle it. For instance:

 public class ThingComparer : IComparer { public int Compare(object x, object y) { // null- and type-checking omitted for clarity // sort by A, B, and then C if (xA != yA) return xACompareTo(yA); if (xB != yB) return xBCompareTo(yB); return xCCompareTo(yC); } } 
+12
source

If you are implementing your own comparator, you can do whatever you want:

 List<Customer> customers = GetCustomers(); customers.Sort(delegate(Customer x, Customer y) { if (x.Name != y.Name) { return x.Name.CompareTo(y.Name); } return x.Location.CompareTo(y.Location); }); 

Now the above code is not an IComparer class, but the comparison method is the same.

0
source
 public class ScratchComparer : IComparer<Scratch> { public int Compare(Scratch x, Scratch y) { return x.Foo.CompareTo(y.Foo).CompareTo(0.CompareTo(x.Bar.CompareTo(y.Bar))); } } [TestFixture] public class Scratch { public virtual int Foo { get; set; } public virtual int Bar { get; set; } [Test] public void Should_sort() { var scratches = new[] { new Scratch {Foo = 1, Bar = 1}, new Scratch {Foo = 2, Bar = 1}, new Scratch {Foo = 1, Bar = 1}, new Scratch {Foo = 1, Bar = 2} }; // IComparer Array.Sort(scratches, new ScratchComparer()); scratches[0].Foo.ShouldEqual(1); scratches[0].Bar.ShouldEqual(1); scratches[1].Foo.ShouldEqual(1); scratches[1].Bar.ShouldEqual(1); scratches[2].Foo.ShouldEqual(1); scratches[2].Bar.ShouldEqual(2); scratches[3].Foo.ShouldEqual(2); scratches[3].Bar.ShouldEqual(1); // better Scratch[] ordered = scratches.OrderBy(x => x.Foo).ThenBy(x => x.Bar).ToArray(); ordered[0].Foo.ShouldEqual(1); ordered[0].Bar.ShouldEqual(1); ordered[1].Foo.ShouldEqual(1); ordered[1].Bar.ShouldEqual(1); ordered[2].Foo.ShouldEqual(1); ordered[2].Bar.ShouldEqual(2); ordered[3].Foo.ShouldEqual(2); ordered[3].Bar.ShouldEqual(1); } } 
0
source

All Articles