Solved

Sorting a HashTable

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

18 Experts available now in Live!

Get 1:1 Help Now