0

I'm trying to sort the existing list which is ICollection<T> but failed. Below is what I've tried. No error but no effect as well.

....
ObjectList.OrderBy(x=>x.Name);
....

Am I totally off from sorting the list using the above way? I also tried:

ObjectList = ObjectList.OrderBy(x=>x.Name);

but this is worse as it's giving me error saying I'm missing a cast.

Is it possible to sort the same list instead of assigning it to another variable?

SuicideSheep
  • 4,542
  • 15
  • 54
  • 99
  • 1
    possible duplicate of [Sorting a list using Lambda/Linq to objects](http://stackoverflow.com/questions/722868/sorting-a-list-using-lambda-linq-to-objects) – Sergio Feb 12 '14 at 08:24
  • what type of `ObjectList`? – Grundy Feb 12 '14 at 08:25
  • 1
    try `ObjectList.ToList().Sort();` – w.b Feb 12 '14 at 08:26
  • you can try [List.Sort](http://msdn.microsoft.com/ru-ru/library/b0zbh7b6(v=vs.110).aspx) – Grundy Feb 12 '14 at 08:27
  • @user2720372: But it's a list of custom object. How to specify which field from `ObjectList.ToList().Sort();`? – SuicideSheep Feb 12 '14 at 08:27
  • @user2720372 then he sorts a temporary list and never uses it. – Dirk Feb 12 '14 at 08:27
  • @Mr.SuicideSheep see this overload [List.Sort Method (Comparison)](http://msdn.microsoft.com/ru-ru/library/w56d4y5z(v=vs.110).aspx) – Grundy Feb 12 '14 at 08:28
  • If its actually a `List` or `T[]` you can use `List.Sort` or `Array.Sort` instead which is more efficient and don't need to create a new collection. You can pass a custom `IComparer`(f.e. which compares by name) or - if you use at least .NET 4.5. - you can use [`Comparer.Create`](http://msdn.microsoft.com/en-us/library/hh737198(v=vs.110).aspx) to create one on the fly. If you're not sure you could use the `as`-operator to try-cast it to a list/array. – Tim Schmelter Feb 12 '14 at 09:17

3 Answers3

3

The short answer is no, it is not possible to sort an ICollection without assigning it to another variable. The ICollection interface does not expose any way to do that.

However sorting with OrderBy as in your example works as excpected, but returns an IEnumerable and not an ICollection.

I would assign it to another variable like so:

var sortedList = ObjectList.OrderBy(x=>x.Name);
Robban
  • 6,483
  • 2
  • 32
  • 41
  • 1
    He could also do `ObjectList = ObjectList.Orderby(x => x.Name).ToList()` since `List` implements `ICollection` but that just avoid having a different variable but still creates a new collection and doesn't sort in-place. – Dirk Feb 12 '14 at 08:31
1

You must add .ToList() at the because OrderBy returns an IEnumerable, so you try as:

ObjectList = ObjectList.OrderBy(x=>x.Name). ToList() ;
Kamil Budziewski
  • 21,193
  • 12
  • 77
  • 95
M.S.
  • 3,974
  • 1
  • 15
  • 37
1

Such a generic sort operation does not exist, because it is unclear how to sort a generic collection. If your collection was List<T>, you could use the Sort method there (because it is possible to sort a list). However, other collections like e.g. hash sets simply do not allow to be sorted, so you will have to create a new collection, if you want your collection to be sorted.

Georg
  • 5,191
  • 19
  • 39