Home >Backend Development >C++ >How to get hashtable elements as sorted array?
A hash table is a non-universal collection of key-value pairs arranged according to the hash code of the key. Hash tables are used to create collections that are stored using hash tables. Hash tables optimize lookups by calculating the hash code for each key and storing it in an internal basket. When we access a specific value from the hash table, the hash code matches the specified key.
This hash table collection is defined in the System.Collections namespace of C#. The class that represents a collection of hash tables is the "Hashtable" class. This class provides constructors, methods, and properties to operate on collections of hash tables. By default, hash table collections are unsorted. If we want a sorted collection of hash tables, we need to represent it as an Array or ArrayList and sort the elements.
In this article, we will see how to get the hash table elements in the form of a sorted array. So let's get started.
We know that by default, the hash table collection is unsorted. Sorting a hash table collection can be very difficult because we create the hash table collection based on the keys and then add values to each key.
If we want to sort a collection of hash tables, we must sort it based on keys or values. There is no direct method in the Hashtable class to sort a collection of hash tables. So we must turn to other methods.
One way is to get the hash table elements (keys or values) as a sorted array. To do this, we will follow the steps listed below.
Create a hash table object
Fill this object with key-value pairs
Create an array of string type, length = length of hash table
Traverse the hash table according to the key and fill the array with each key
Sort the generated array
We wrote this method using C# as shown below.
using System; using System.Collections; class Program { public static void Main() { // Create a Hashtable Hashtable langCodes = new Hashtable(); // Add elements to the Hashtable langCodes.Add("C++", "CPlusPlus"); langCodes.Add("C#", "CSharp"); langCodes.Add("Java", "Java"); langCodes.Add("PL", "Perl"); langCodes.Add("PG", "Prolog"); int k = langCodes.Count; // create array of length = hashtable length string[] sortedArray = new string[k]; // Retrieve key values in Array int i = 0; Console.WriteLine("Hashtable langCodes Contents:"); foreach (DictionaryEntry de in langCodes) { Console.WriteLine("{0} ({1}) ", de.Key, de.Value); sortedArray[i] = de.Key.ToString(); i++; } Array.Sort(sortedArray); Console.WriteLine("Contents of sorted array based on Hashtable keys:"); foreach (var item in sortedArray) { Console.WriteLine(item); } } }
In this program, we define a Hashtable object langCodes and populate it with key-value pairs. We then retrieve the length of the hash table and use Declare an array "sortedArray" of this length. Next we traverse langCodes hash table and populate the sorted array object with keys langCodes Hash table values.
Then we use the filter Array.Sort(sortedArray) to sort the array and print it This sorted array.
The output of the program is as follows.
Hashtable langCodes Contents: PG (Prolog) Java (Java) C# (CSharp) PL (Perl) C++ (CPlusPlus) Contents of sorted array based on Hashtable keys: C# C++ Java PG PL
From the output, we can see that the hash table key elements are retrieved as a sorted array of elements.
Now let’s give another example. We use the same method discussed above. The only difference is that in this example we will populate the array with values instead of keys from a hash table.
Let’s look at the complete C# program.
using System; using System.Collections; class Program { public static void Main() { // Create a Hashtable Hashtable numberNames = new Hashtable(); // Add elements to the Hashtable numberNames.Add(12, "Twelve"); numberNames.Add(2, "Two"); numberNames.Add(65, "Sixty Five"); numberNames.Add(15, "Fifteen"); numberNames.Add(18, "Eighteen"); int k = numberNames.Count; //create array of length = hashtable length string[] sortedArray = new string[k]; // Retrieve hashtable values in array. int i = 0; Console.WriteLine("Hashtable langCodes Contents:"); foreach (DictionaryEntry de in numberNames) { Console.WriteLine("{0} ({1}) ", de.Key, de.Value); sortedArray[i] = de.Value.ToString(); i++; } Array.Sort(sortedArray); Console.WriteLine("Contents of sorted array based on Hashtable values:"); foreach (var item in sortedArray) { Console.WriteLine(item); } } }
This program has a hash table object, numberNames. We fill it with numbers and their corresponding number names. By traversing the hash table, we fill the sorted array with values. We then sort the array using the Array.Sort() filter and print the sorted array.
The output of the program is shown below.
Hashtable langCodes Contents: 18 (Eighteen) 12 (Twelve) 65 (Sixty Five) 2 (Two) 15 (Fifteen) Contents of sorted array based on Hashtable values: Eighteen Fifteen Sixty Five Twelve Two
From the output, we can see that the contents of the array (the values in the hash table) are indeed sorted alphabetically. We can easily compare the output of the hash table and the sorted array. In a hash table, the output is unsorted. Key-value pairs are displayed randomly. When in an array, the output is sorted.
Although sorting hash table elements is difficult, we can do it by representing hash table elements as an array. But it is not possible to sort keys and values at the same time. We can retrieve all keys in an array or all values in an array. We can then sort the array using the Array.Sort() filter. We can also convert the hash table to an array or list of arrays and process it.
The above is the detailed content of How to get hashtable elements as sorted array?. For more information, please follow other related articles on the PHP Chinese website!