Sort array of items using OrderBy<>

98,385

Solution 1

To be clear, OrderBy won't sort the array in place - it will return a new sequence which is a sorted copy of the array. If that's okay, then you want something like:

var sorted = array.OrderBy(item => item.Fields["FieldName"].Value);

On the other hand, I don't understand your comment that the property is returned as a string but that you can cast it to an int - you can't cast strings to ints, you have to parse them. If that's what you meant, you probably want:

var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value));

If you want that as an array, you can call ToArray() afterwards:

var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value))
                  .ToArray();

Alternatively you could use Array.Sort if you want to sort in-place, but that will be somewhat messier.

Solution 2

Use the Sort method to sort an array:

Array.Sort(theArray, (a, b) => String.Compare(a.Fields["FieldName"].Value, b.Fields["FieldName"].Value));

If you are not using C# 3, you use a delegate instead of a lambda expression:

Array.Sort(theArray, delegate(Item a, Item b) { return String.Compare(a.Fields["FieldName"].Value, b.Fields["FieldName"].Value); } );

(This also works with framework 2, which the OrderBy extension doesn't.)

Solution 3

If you can use orderby it should be easy, try the following. I threw in the int.Parse although depending on how you actually want to sort this might not be required.

var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value));

Solution 4

var sortedArray = items.OrderBy(i => i.property).ToArray();

If you don't want an array, you can leave that off in which case you will have an IEnumerable<> of type item.

Solution 5

It's worth mentioning that List<T>.Sort is based on quick sort, and in most cases it is not a stable sort.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

However you can use Enumberable.OrderBy which performs a stable sort.

This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.

Share:
98,385

Related videos on Youtube

Zooking
Author by

Zooking

Sitecore developer with lots of different CMS-experience.

Updated on July 09, 2022

Comments

  • Zooking
    Zooking almost 2 years

    I have an array of items and I would like to sort on one of their properties. I can access the items property using "item.Fields["FieldName"].Value" the property is returned as a string but I can cast it to an int.

    I had a look at OrderBy<> but I have no idea of how to use it.

  • rahularyansharma
    rahularyansharma almost 12 years
    @skeet i am facing almost same problem and i have posted question also stackoverflow.com/questions/11693827/…
  • Kevin Meredith
    Kevin Meredith almost 11 years
    Thanks, Jon. Good to know it's a non-destructive operation
  • toddmo
    toddmo over 10 years
    one line of code, no extra variables. this should really be the answer.
  • AlbatrossCafe
    AlbatrossCafe over 8 years
    what library do you have to include to user OrderBy ?
  • Jon Skeet
    Jon Skeet over 8 years
    @AlbatrossCafe: just a using directive for System.Linq and a reference to the System.Core assembly. (It may have moved now... Not sure.)
  • AlbatrossCafe
    AlbatrossCafe over 8 years
    @JonSkeet You are right, it was System.Linq. Thanks!