Support we have a List of Tuples, and we want to sort based on the first value of each tuple, and then the second value. Here we use an ascending and also descending sort.
Module Module1
Function GetData() As List(Of Tuple(Of Integer, Integer))
Dim result As New List(Of Tuple(Of Integer, Integer))
result.Add(New Tuple(Of Integer, Integer)(100, 1))
result.Add(New Tuple(Of Integer, Integer)(100, 2))
result.Add(New Tuple(Of Integer, Integer)(10, 2))
result.Add(New Tuple(Of Integer, Integer)(10, 1))
Return result
End Function
Function ComparisonTwoTuples(ByVal tupleA As Tuple(Of Integer, Integer),
ByVal tupleB As Tuple(Of Integer, Integer)) As Integer
' Compare the first Item of each tuple in ascending order.
Dim part1 As Integer = tupleA.Item1
Dim part2 As Integer = tupleB.Item1
Dim compareResult As Integer = part1.CompareTo(part2)
' If not equal, return the comparison result.
If compareResult <> 0 Then
Return compareResult
End If
' Compare the second item of each tuple in descending order.
Return tupleB.Item2.CompareTo(tupleA.Item2)
End Function
Sub Main()
Dim data As List(Of Tuple(Of Integer, Integer)) = GetData()
data.Sort(New Comparison(Of Tuple(Of Integer, Integer))(AddressOf ComparisonTwoTuples))
For Each value In data
Console.WriteLine(
"SORTED ON 2 VALUES: {0}", value)
Next
End Sub
End Module
SORTED ON 2 VALUES: (10, 2)
SORTED ON 2 VALUES: (10, 1)
SORTED ON 2 VALUES: (100, 2)
SORTED ON 2 VALUES: (100, 1)