score:2
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.
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- multiple orderby in this linq code
- LINQ Lambda efficiency of code groupby orderby
- LINQ to SQL: orderby a sum operation
- creating Linq to sqlite dbml from DbLinq source code
- Linq - 'Saving' OrderBy operation (c#)
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>
- LINQ Orderby Descending Query
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ OrderBy versus ThenBy
- Linq code to select one item
- How do I specify the Linq OrderBy argument dynamically?
- LINQ OrderBy with more than one field
- How are people unit testing code that uses Linq to SQL
- Linq OrderBy against specific values
- LINQ OrderBy is not sorting correctly
- Custom sort logic in OrderBy using LINQ
- LINQ orderby on date field in descending order
- C# LINQ Orderby - How does true/false affect orderby?
- Use own IComparer<T> with Linq OrderBy
- C# Linq OrderBy filtering null or empty values to be last
- How to OrderBy an integer in a string field in a Linq query
- LINQ OrderBy not ordering .. changing nothing .. why?
- Linq syntax for OrderBy with custom Comparer<T>
- How to apply multiple orderby in linq query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Why does a Linq Cast<T> operation fail when I have an implicit cast defined?
- Retry Policy for LINQ to SQL Stored Procedure calls
- IList dataset insert to database table
- Convert SQL query to linq group by
- Your Favorite LINQ-to-Objects Queries
- Linq multiple join ,then GroupBy along with OrderBy , tried everything I could find
- LINQ - Group List<T> to Year, Month, Day
- RavenDB Session > 30
- How to begin creating Linq-To-Repository to HttpRequest framework
- LINQ - Complex OrderBy
- How to fix 'No property or field exists in type' error?
- How to use Max and Group By in an NHibernate query?
- LINQ filter on table to meet several criteria / DateDifference
- Why is Entity Framework returning null?
- LINQ query: restrict child entity
- linq count by 2 conditions
- Binary Search versus LINQ select statement
- how to format a string in linq
- Get the max value of a property in a collection or return zero when the sequence is empty
- Using LINQ. How can I compare two simple lists to give another list of objects in the new and not in the old list?
- C# LINQ method to determine if array is subset of another (including duplicates)?