Solved

Sorting a HashTable

Posted on 2006-06-13
2
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
neilprice earned 75 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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