Verification: a143cc29221c9be0

Order by function in php

OrderBy

OrderBy sorts the values of a collection in ascending or descending order. It sorts the collection in ascending order by default because ascending keyword is optional here. Use descending keyword to sort collection in descending order.

IListStudent> studentList = new ListStudent>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
};

var orderByResult = from s in studentList
                   orderby s.StudentName 
                   select s;

var orderByDescendingResult = from s in studentList
                   orderby s.StudentName descending
                   select s;
Dim orderByResult = From s In studentList
                   Order By s.StudentName  
                   Select s

Dim orderByDescendingResult = From s In studentList
                   Order By s.StudentName Descending
                   Select s

orderByResult in the above example would contain following elements after execution:

Bill
John
Ram
Ron
Steve

orderByDescendingResult in the above example would contain following elements after execution:

Steve
Ron
Ram
John
Bill

OrderBy in Method Syntax

OrderBy extension method has two overloads. First overload of OrderBy extension method accepts the Func delegate type parameter. So you need to pass the lambda expression for the field based on which you want to sort the collection.

The second overload method of OrderBy accepts object of IComparer along with Func delegate type to use custom comparison for sorting.

public static IOrderedEnumerable OrderBy(this IEnumerable source, 
            Func keySelector);

public static IOrderedEnumerable OrderBy(this IEnumerable source, 
            Func keySelector, 
            IComparer comparer);

The following example sorts the studentList collection in ascending order of StudentName using OrderBy extension method.

IListStudent> studentList = new ListStudent>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
};

var studentsInAscOrder = studentList.OrderBy(s => s.StudentName);
Dim studentsInAscOrder = studentList.OrderBy(Function(s) s.StudentName)

Method syntax does not allow the decending keyword to sorts the collection in decending order. Use OrderByDecending() method for it.

OrderByDescending

OrderByDescending sorts the collection in descending order.

OrderByDescending is valid only with the Method syntax. It is not valid in query syntax because the query syntax uses ascending and descending attributes as shown above.

IListStudent> studentList = new ListStudent>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
};

var studentsInDescOrder = studentList.OrderByDescending(s => s.StudentName);
Dim studentsInDescOrder = studentList.OrderByDescending(Function(s) s.StudentName)

A result in the above example would contain following elements after execution.

Steve
Ron
Ram
John
Bill

Please note that OrderByDescending is not supported in query syntax. Use the decending keyword instead.