Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a HashTable

Posted on 2006-06-13
2
Medium Priority
?
263 Views
Last Modified: 2010-04-23
I have a hashtable with a list of songs, which is in a random order (but with consecutive keys):

444, Slayer - Epedemic
445, Dragonforce - Fury of the Storm
446, Metallica - For Whom The Bell Tolls

This is written to a set of <option></option> tags for a select in a HTML page, as follows:

<select name="track">
<option value="444">Slayer - Epedemic</option>
<option value="445">Dragonforce - Fury of the Storm</option>
<option value="446">Metallica - For Whom The Bell Tolls</option>
</select>

However, this isn't much use to users, as the songs are all over the place.

Dim trackList As String = ""
For i = 0 To trackNames.Count
    If trackNames.Item(CStr(i)) <> "" Then trackList = trackList & "<option value=" & Chr(34) & CStr(i) & Chr(34) & IIf(i = activeTrack, " selected", "") & ">" & trackNames.Item(CStr(i)) & "</option>" & vbNewLine
Next

Where trackNames is the hashtable. I also have another hashtable (called trackFiles) which has the same set of keys as trackNames, providing the file paths for the matching track names.

How can I change my code to list the track names in alphabetical order but keep the keys intact?
0
Comment
Question by:burningmace
2 Comments
 
LVL 6

Accepted Solution

by:
neilprice earned 300 total points
ID: 16896579
Here's an example code for accomplishing this taking the values from my hashtable h and sorting them...

Hope this helps,
Neil

Imports System.Collections

Module Module1

    Sub Main()

        Dim h As Hashtable = New Hashtable()

        h.Add(10, "Gregory")
        h.Add(11, "Simon")
        h.Add(12, "Anthony")
        h.Add(13, "Zebbede")
        h.Add(14, "Billy")

        Dim list As List(Of String) = New List(Of String)

        For Each i As DictionaryEntry In h
            list.Add(i.Value + "///" + i.Key.ToString()) 'Make a unique seperator for the middle here
        Next

        list.Sort()

        Dim mysong As String = ""
        Dim mykey As String = ""

        For Each s As String In list
            mysong = s.Substring(0, s.IndexOf("///"))
            mykey = s.Substring(s.IndexOf("///") + 3)
            Console.WriteLine(mykey.ToString() + " -- " + mysong.ToString())
        Next

        Console.ReadLine()
    End Sub

End Module
0
 
LVL 5

Author Comment

by:burningmace
ID: 16897056
Cheers. Got it implemented. Works brilliantly.

I didn't know that you could cast elements of a Hashtable to a DictionaryEntry! Could be useful in future.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question