The following code shows you sorting arrays in ascending and descending orders using LINQ.
Array In Ascending Order:
Imports System.Linq
Dim array As Integer() = New Integer() {2, 1, -1, 0, 10, 4, 6, 11}
Dim query = _
From i As Integer In array _
Order By i Ascending _
Select i
For Each value As Integer In query
Console.Write(value.ToString() & " ")
Next
Output:
-1 0 1 2 4 6 10 11
Or,
Dim strArray As String() = New String() {"D", "E", "A", "B", "Z", "X"}
Dim query = _
From i As String In strArray _
Order By i Ascending _
Select i
For Each value As String In query
Console.Write(value & " ")
Next
Output:
A B D E X Z
Array In Descending Order:
Dim query2 = _
From i As Integer In array _
Order By i Descending _
Select i
For Each value As Integer In query2
Console.Write(value.ToString() & " ")
Next
Output:
11 10 6 4 2 1 0 -1
Or,
Dim strArray As String() = New String() {"D", "E", "A", "B", "Z", "X"}
Dim query = _
From i As String In strArray _
Order By i Descending _
Select i
For Each value As String In query
Console.Write(value & " ")
Next
Z X E D B A
0 comments:
Post a Comment