score:2

Accepted answer

Use a Func delegate to store your ordering then pass that to the OrderBy method:

Func<int, int> orderFunc = i => i; // func for ordering
var list = Enumerable.Range(1,10).OrderByDescending(i => i); // 10, 9 ... 1
var newList = list.OrderBy(orderFunc); // 1, 2 ... 10

As another example consider a Person class:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now you want to preserve a sort order that sorts by the Name property. In this case the Func operates on a Person type (T) and the TResult will be a string since Name is a string and is what you are sorting by.

Func<Person, string> nameOrder = p => p.Name;

var list = new List<Person>
{
    new Person { Id = 1, Name = "ABC" },
    new Person { Id = 2, Name = "DEF" },
    new Person { Id = 3, Name = "GHI" },
};

// descending order by name
foreach (var p in list.OrderByDescending(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 3:GHI
// 2:DEF
// 1:ABC

// re-assinging the list
list = new List<Person>
{
    new Person { Id = 23, Name = "Foo" },
    new Person { Id = 14, Name = "Buzz" },
    new Person { Id = 50, Name = "Bar" },
};

// reusing the order function (ascending by name in this case)
foreach (var p in list.OrderBy(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 50:Bar
// 14:Buzz
// 23:Foo

EDIT: be sure to add ToList() after the OrderBy calls if you need a List<T> since the LINQ methods will return an IEnumerable<T>.

score:1

Calling ToList() or ToArray() on your IEnumerable<T> will cause it to be immediately evaluated. You can then assign the resulting list or array to "save" your ordered list.


Related Articles