Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an IEnumerable<string>

How can I sort an IEnumerable<string> alphabetically. Is this possible?

Edit: How would I write an in-place solution?

like image 490
CatZilla Avatar asked Sep 02 '10 19:09

CatZilla


People also ask

How to order by string in c#?

Firstly, set a string array. string[] values = { "tim", "amit", "tom", "jack", "saurav"}; Use the Sort() method to sort.

How OrderBy works c#?

In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element.

How do I sort a list in ascending order LINQ?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

Does IEnumerable guarantee order?

IEnumerable/IEnumerable<T> makes no guarantees about ordering, but the implementations that use IEnumerable/IEnumerable<T> may or may not guarantee ordering.


2 Answers

The same way you'd sort any other enumerable:

var result = myEnumerable.OrderBy(s => s); 

or

var result = from s in myEnumerable              orderby s              select s; 

or (ignoring case)

var result = myEnumerable.OrderBy(s => s,                                   StringComparer.CurrentCultureIgnoreCase); 

Note that, as is usual with LINQ, this creates a new IEnumerable<T> which, when enumerated, returns the elements of the original IEnumerable<T> in sorted order. It does not sort the IEnumerable<T> in-place.


An IEnumerable<T> is read-only, that is, you can only retrieve the elements from it, but cannot modify it directly. If you want to sort a collection of strings in-place, you need to sort the original collection which implements IEnumerable<string>, or turn an IEnumerable<string> into a sortable collection first:

List<string> myList = myEnumerable.ToList(); myList.Sort(); 

Based on your comment:

_components = (from c in xml.Descendants("component")                let value = (string)c                orderby value                select value               )               .Distinct()               .ToList(); 

or

_components = xml.Descendants("component")                  .Select(c => (string)c)                  .Distinct()                  .OrderBy(v => v)                  .ToList(); 

or (if you want to later add more items to the list and keep it sorted)

_components = xml.Descendants("component")                  .Select(c => (string)c)                  .Distinct()                  .ToList();  _components.Add("foo"); _components.Sort(); 
like image 166
dtb Avatar answered Oct 02 '22 21:10

dtb


It is impossible, but it isn't.

Basically, any sort method is going to copy your IEnumerable into a List, sort the List and then return to you the sorted list, which is an IEnumerable as well as an IList.

This means you lose the "continue infinitely" property of an IEnumerable, but then you couldn't sort one like that anyway.

like image 27
James Curran Avatar answered Oct 02 '22 20:10

James Curran