OrderBy method (with objects)
Description
The OrderBy method sorts the elements of a sequence in ascending order.
Sample
This sample sort a list of Person objects by the Age property in ascending order.
var data := List<Person>{}{ ;
Person{}{Name := "Jon Doe", Age := 40}, ;
Person{}{Name := "Jane Doe", Age := 20}, ;
Person{}{Name := "Joe Schmoe", Age := 30} ;
}
var result := data:OrderBy({ q => q:age })
foreach var item in result
Console.WriteLine(String.Format("{0} ({1})", item:Name, item:Age))
next
Output
Jane Doe (20)
Joe Schmoe (30)
Jon Doe (40)
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<Person>{}{ ;
Person{}{Name := "Jon Doe", Age := 40}, ;
Person{}{Name := "Jane Doe", Age := 20}, ;
Person{}{Name := "Joe Schmoe", Age := 30} ;
}
var result := data:OrderBy({ q => q:age })
foreach var item in result
Console.WriteLine(String.Format("{0} ({1})", item:Name, item:Age))
next
return
class Person
public property Name as string auto
public property Age as int auto
end class
var data := List<Person>{}{ ;
Person{}{Name := "Jon Doe", Age := 40}, ;
Person{}{Name := "Jane Doe", Age := 20}, ;
Person{}{Name := "Joe Schmoe", Age := 30} ;
}
var result := from q in data ;
orderby q:Age ;
select q
foreach var item in result
Console.WriteLine(String.Format("{0} ({1})", item:Name, item:Age))
next
Output
Jane Doe (20)
Joe Schmoe (30)
Jon Doe (40)
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<Person>{}{ ;
Person{}{Name := "Jon Doe", Age := 40}, ;
Person{}{Name := "Jane Doe", Age := 20}, ;
Person{}{Name := "Joe Schmoe", Age := 30} ;
}
var result := from q in data ;
orderby q:Age ;
select q
foreach var item in result
Console.WriteLine(String.Format("{0} ({1})", item:Name, item:Age))
next
return
class Person
public property Name as string auto
public property Age as int auto
end class