How can I sort an IEnumerable alphabetically. Is this possible?
Edit: How would I write an in-place solution?
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
An IEnumerable
List 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();