Sorting array with respect to two properties I'm sorting with respect to Width and Length. However, Width has the significance precedence, so Length is only vital if Width is exactly equal (both are integers). The comparison method I've implemented as shown below. Is this a neat trick with doubling the most significant value's comparer or is there a better way?

  List&lt;Thing&gt; things = thingsDictionary.Select(thing 
    =&gt; thing.Value).ToList();
  things.Sort((first, second)
    =&gt; 2 * first.Width.CompareTo(second.Width)
       + first.Length.CompareTo(second.Length));
It looks like your implementation relies on Width.CompareTo(second.Width) to return either 1, 0, or -1.

This assumption is wrong. If first.Length is greater than second.Length, CompareTo is allowed to return 9999, or 1, or 500. These could very well screw up your additions.

Sure, the current .NET implementation returns either 1, 0, or -1, but that's an implementation detail. Furthermore, other C# implementations or even future version of .NET may return other values.

Also, tricks reduce readability.

A correct implementation would be:

things.Sort((first, second)
    =&gt; {
        var widthComparison = first.Width.CompareTo(second.Width);
        if(widthComparison == 0)
            return widthComparison;
        else return first.Length.CompareTo(second.Length);
    });


But instead of fixing your implementation, I would suggest using LINQ's OrderBy and ThenBy instead. It doesn't seem like you need to sort the list in-place, so there's no need to reinvent the wheel.

List&lt;Thing&gt; things = thingsDictionary.Select(thing =&gt; thing.Value)
                                     .OrderBy(x =&gt; x.Width)
                                     .ThenBy(x =&gt; x.Length)
                                     .ToList();