C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: It returns the result of CompareTo on the Length properties of the String parameters.
Tip: This means the List is sorted in ascending order by the lengths of its strings, from shortest to longest.
LambdaVB.NET program that uses Sort function with lambda
Module Module1
Sub Main()
Dim l As List(Of String) = New List(Of String)
l.Add("mississippi")
l.Add("indus")
l.Add("danube")
l.Add("nile")
' Sort using lambda expression.
l.Sort(Function(elementA As String, elementB As String)
Return elementA.Length.CompareTo(elementB.Length)
End Function)
For Each element As String In l
Console.WriteLine(element)
Next
End Sub
End Module
Output
nile
indus
danube
mississippi
Finally: We loop through the sorted List—the strings are now in alphabetical order.
VB.NET program that uses Sort function on List
Module Module1
Sub Main()
Dim l As List(Of String) = New List(Of String)
l.Add("tuna")
l.Add("velvetfish")
l.Add("angler")
' Sort alphabetically.
l.Sort()
For Each element As String In l
Console.WriteLine(element)
Next
End Sub
End Module
Output
angler
tuna
velvetfish
VB.NET program that uses Reverse function
Module Module1
Sub Main()
Dim l As List(Of String) = New List(Of String)
l.Add("anchovy")
l.Add("barracuda")
l.Add("bass")
l.Add("viperfish")
' Reverse list.
l.Reverse()
For Each element As String In l
Console.WriteLine(element)
Next
End Sub
End Module
Output
viperfish
bass
barracuda
anchovy
Note: Instead of using String instances in your List, you can use other types such as Integer or even Class types.