I have a List of objects in C#. All the objects contain properties code1 and code2 (among other properties). The list of objects is in no particular order. I need to sort the li
Note that Adam Houldsworth's answer with the .ToList()
call needlessly creates a new list. If your list is large, this may create unacceptable memory pressure. It would most likely be better to sort the list in place by providing a custom comparison function:
theList.Sort((a, b) =>
{
var firstCompare = a.code1.CompareTo(b.code1);
return firstCompare != 0 ? firstCompare : a.code2.CompareTo(b.code2);
});
Alternatively, if this ordering is an intrinsic property of your type, you could implement IComparable
on your type, and just call
theList.Sort();
... which will use the IComparable
implementation.