16

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 list of objects by their code1 and code2 properties.

Example:

List -> object = id, name, code1, code2, hours, amount.

Example code 1 = 004
Example code 2 = 001, 002, 003, 004, 016
Example code 1 = 005
Example code 2 = 001, 002, 003, 004

So after the sort I would want the objects in the following order

004 001
004 002
004 003
004 005
004 016
005 001
005 002
005 003
005 004
1
  • 1
    +1 Not sure why you were downvoted. Commented Apr 4, 2012 at 14:34

2 Answers 2

27

You could use linq extensions (leaving the original list unsorted):

var sorted = theList.OrderBy(o => o.code1).ThenBy(o => o.code2);

To replace the original list with a sorted one, make a slight amendment (not very efficient, it creates a new list):

theList = theList.OrderBy(o => o.code1).ThenBy(o => o.code2).ToList();

This assumes that your list is of the correct type, something like:

List<MyClass> theList = new List<MyClass>();

And not a list of objects, in which case you would need to make use of .Cast<>() or .OfType<>().

Sign up to request clarification or add additional context in comments.

3 Comments

Note that this leaves the original list unsorted.
The ToList call, as I imagine you are aware, still does not sort the original list; it creates a new list with the items in order. That will most likely be slightly slower than sorting the list in place; it will certainly require more memory, and if the list is large, the extra memory pressure may have a significant impact on performace.
@phoog Fair enough - I held off providing an implementation of the Sort method as I couldn't be confident of it without compiling and trying it. I've amended my answer to be a little more explicit.
11

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<T> on your type, and just call

theList.Sort();

... which will use the IComparable<T>.CompareTo() implementation.

1 Comment

@AdamHouldsworth And of course your approach gives neater code. I'd probably use Linq if I were certain that the list would never contain more than a handful of items, since performance would not likely be an issue in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.