Solved

Sorting a HashTable

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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