Home

Sorting a List

     

Introduction

 

Sorting a list consists of re-arranging its members in a certain order. The arrangement depends on the type of values of the list. That is, the list can be arranged in ascending order, in chronological order, in incremental order, or in logical order.

 

Ascending Order

When you create a list, you add the items in any order of your choice. When you create a Select statement, the items are added to its list in the order they appear in the main list. When treating the new list or when presenting it to the user, you may want to arrange it in alphabetical, numerical, or chronological order.

To support this operation, the LINQ provides the Order By operator. To apply it, write the operator before the Select operation followed by the From list. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Order By N
                     Select N

        for each  Member in Number
            Console.WriteLine(member.ToString())
        next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Order By

If you apply the Order By operator simply followed by a variable, the list is ordered alphabetically or numerically depending on the types of values in the list. This is referred to as ascending. To re-enforce this, you can follow the variable with the Ascending keyword. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Order By N Ascending
                     Select N

        for each  Member in Number
            Console.WriteLine(member.ToString())
        next

        Console.WriteLine()
        Return 0
    End Function
End Module

Descending Order

You can arrange a list in reverse ascending order, in decremental order, or in reverse chronological order. To support this, the LINQ uses the Order By keyword in combination with the Descending keyword. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Order By N Descending
                     Select N

        for each  Member in Number
            Console.WriteLine(member.ToString())
        next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Order By Descending

 
 
 

Sorting With Class

Sorting the members of a primitive-based list is quite easy. This is because the classes (or structures) of each data type implement the IComparable interface. This also makes it easy to sort the values of a Select statement. This means that, to arrange the list of values, in the Order By statement, type the name of the from variable and use the period operator to specify the base of what member you want to arrange the list. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee = {
            New Employee(971974, "Patricia", "Katts", 24.68),
            New Employee(208411, "Raymond", "Kouma", 20.15),
            New Employee(279374, "Hélène", "Mukoko", 15.55),
            New Employee(707912, "Bertrand", "Yamaguchi", 24.68),
            New Employee(971394, "Gertrude", "Monay", 20.55)
        }

        Dim Empls = From StaffMembers
                    In employees
                    Order By StaffMembers.EmployeeNumber
                    Select StaffMembers

        Console.WriteLine("+========+============+===========+========+")
        Console.WriteLine("| Empl # | First Name | Last Name | Salary |")
        Console.WriteLine("+=======+============+===========+=========+")
        For Each Person In Empls
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |",
            		      Person.EmployeeNumber, Person.FirstName, 
                	      Person.LastName, Person.HourlySalary)
            Console.WriteLine("+--------+------------+-----------+--------+")
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

+========+============+===========+========+
| Empl # | First Name | Last Name | Salary |
+=======+============+===========+=========+
| 208411 | Raymond    | Kouma     |  20.15 |
+--------+------------+-----------+--------+
| 279374 | Hélène     | Mukoko    |  15.55 |
+--------+------------+-----------+--------+
| 707912 | Bertrand   | Yamaguchi |  24.68 |
+--------+------------+-----------+--------+
| 971394 | Gertrude   | Monay     |  20.55 |
+--------+------------+-----------+--------+
| 971974 | Patricia   | Katts     |  24.68 |
+--------+------------+-----------+--------+

Press any key to continue . . .

In the same way, you can specify by what member of the class the list should be sorted.

 
 
   
 

Previous Copyright © 2010-2016, FunctionX Next