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 SortedList

Use the SortedList collection to store keys and values, sorted on the keys.
SortedList. Some collections keep their element sorted as you use them. The SortedList type, for example, always sorts its keys. We add keys and values to SortedList.Sort
Unlike a List, the SortedList stores both keys and values. For a collection with just keys (that is more like a List, except no duplicates) consider a SortedSet.SortedSet
First example. Here we create a SortedList, and then invoke Add(). In Add, the first argument is the key to add. The second argument is the value at that key.

For Each: We use the For-Each loop over the SortedList. The KeyValuePair is used (with matching types to the SortedList) in the loop.

For Each, ForKeyValuePair

Tip: The SortedList has no ways to reorder its elements. If you want to change the ordering, you must create a new collection.

VB.NET program that uses SortedList Module Module1 Sub Main() ' Create SortedList and add Strings to it. Dim sorted As SortedList(Of String, String) = New SortedList(Of String, String) sorted.Add("dog", "red") sorted.Add("cat", "black") sorted.Add("zebra", "black and white") sorted.Add("ant", "black") ' Loop over pairs in the collection. For Each pair As KeyValuePair(Of String, String) In sorted Console.WriteLine(pair.Key + "/" + pair.Value) Next End Sub End Module Output ant/black cat/black dog/red zebra/black and white
IndexOfKey. We can search for keys and values in the SortedList. To find a key, use IndexOfKey. The special value -1 is returned if the key does not exist—no exception is thrown.

IndexOfValue: Use this function to find a value in the SortedList. The special value -1 is used here too to mean "missing."

VB.NET program that uses IndexOfKey, IndexOfValue Module Module1 Sub Main() Dim items = New SortedList(Of String, Integer) items.Add("bird", 20) items.Add("fish", 0) ' Display all pairs. For Each item As KeyValuePair(Of String, Integer) In items Console.WriteLine("ITEM: {0}", item) Next ' Find this key. Dim indexFish = items.IndexOfKey("fish") Console.WriteLine("INDEX OF FISH: {0}", indexFish) ' Find this value. Dim index20 = items.IndexOfValue(20) Console.WriteLine("INDEX OF 20: {0}", index20) ' If a key or value is missing, the value -1 is returned. Dim indexMissing = items.IndexOfKey("carrot") Console.WriteLine("INDEX OF CARROT: {0}", indexMissing) End Sub End Module Output ITEM: [bird, 20] ITEM: [fish, 0] INDEX OF FISH: 1 INDEX OF 20: 0 INDEX OF CARROT: -1
Notes, duplicates. If we try to add a duplicate to the SortedList, an exception is thrown. We cannot call Add without first checking for the key in the List (consider IndexOfKey).Duplicates
A summary. The SortedList is a sorted list of pairs. It is not used in the same way as a List, which makes it harder to apply in programs. A SortedSet is often a better choice.List
© 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