TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Sort List (Lambda That Calls CompareTo)

Use Sort and Reverse on Lists. Sort is called with a lambda that uses CompareTo.
Sort List. A List can be sorted. The List type has a useful subroutine called Sort that will sort your List either by a default sorting order or with a custom one. And we look at some examples of Sort.
Sort with lambda. This is the most advanced example on this page. Here, we create a List of strings, and then call Sort with a lambda expression argument. Our lambda expression begins with the Function keyword and receives two String parameters.

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.

Lambda
VB.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
Simple example. This example first creates a List of three String instances. Then it invokes the Sort subroutine. This subroutine does not return a value. Instead it changes the List to be sorted.

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
Reverse List. The Reverse subroutine is similar in some ways to the Sort subroutine. It inverts the order of the elements in the List from their original order. It does no sorting. As with Sort, you do not need to assign anything to the result.
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
Summary. This article showed how you can sort the elements in a List using the parameterless Sort subroutine and also with a lambda expression. We also saw how to Reverse the order of the elements in your List.

Note: Instead of using String instances in your List, you can use other types such as Integer or even Class types.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf