Solved

Sorting a HashTable

Posted on 2006-06-13
2
256 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 47
Showdialog 8 29
Spacing between controls 4 30
VB.Net Serial COM1 Port - how to send this text via Serial COM port? 13 41
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now