How to LINQ order a collection

I have a set of class objects:

Tests 

This collection contains many instances of Test :

 public class Test { public string column1 { get; set; } } 

I would like to use LINQ to order the contents of Tests and put in a new collection called TestsOrdered . I want to order the contents of column1 . I would like to do this with LINQ, since later I want to add more to the ordering.

How to do it with LINQ.

+7
source share
5 answers

LINQ:

 var result = from test in tests orderby test.column1 select test; 

Free:

 var result = tests.OrderBy(x => x.column1); 
+2
source

Use OrderBy or OrderByDescending (if you want to sort in a downward direction)

 var TestsOrdered = tests.OrderBy(x => x.column1); 
+4
source
 List<Test> testList = new List<Test>(); // .. fill your list testList = testList.OrderBy(x => x.column1).ToList(); 
+2
source
 var TestsOrdered = Tests.OrderBy( t => t.column1 ); 
+1
source

Can you do it like this:

 var TestsOrdered = Tests.OrderBy( t => t.column1 ); 
+1
source

All Articles