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. |
|
|||
|
|