Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Visual Basic

Sorting Hashtable

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
5 Jun 2009CPOL 52.5K   9   5
This short article explains how to use DataView to sort a Hashtable

Introduction

I like using Hashtable, but unfortunately it does not come with Sort functionality. This short article explains how to use DataView to sort the Hashtable. This method allows you to sort not only by key and by value, but also in descending or ascending order.

Using the Code

Here is the function that takes a Hashtable, converts it to a DataView and then sorts it (value DESC, key ASC).

VB.NET
Private Function SortHashtable(ByVal oHash As Hashtable) As DataView
    Dim oTable As New Data.DataTable
    oTable.Columns.Add(New Data.DataColumn("key"))
    oTable.Columns.Add(New Data.DataColumn("value"))

    For Each oEntry As Collections.DictionaryEntry In oHash
        Dim oDataRow As DataRow = oTable.NewRow()
        oDataRow("key") = oEntry.Key
        oDataRow("value") = oEntry.Value
        oTable.Rows.Add(oDataRow)
    Next

    Dim oDataView As DataView = New DataView(oTable)
    oDataView.Sort = "value DESC, key ASC"

    Return oDataView
End Function

Note that there are many sorting combinations you can have like:

  1. value ASC, key ASC
  2. value DESC, key DESC
  3. value DESC, key ASC
  4. value ASC, key DESC
  5. key ASC, value DESC
  6. key DESC, value ASC

Here is the code that uses this SortHashtable() function:

VB.NET
Dim oHash As New Hashtable
oHash.Add("Anna", "2")
oHash.Add("Sam", "3")
oHash.Add("Mary", "5")
oHash.Add("Alice", "4")
oHash.Add("Nicole", "2")

Dim oDataView As DataView = SortHashtable(oHash)
For iRow As Long = 0 To oDataView.Count - 1
    Dim sKey As String = oDataView(iRow)("key")
    Dim sValue As String = oDataView(iRow)("value")
Next

History

  • 5th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Igor is a business intelligence consultant working in Tampa, Florida. He has a BS in Finance from University of South Carolina and Masters in Information Management System from University of South Florida. He also has following professional certifications: MCSD, MCDBA, MCAD.

Comments and Discussions

 
GeneralMy vote of 5 Pin
_Vitor Garcia_28-May-13 4:59
_Vitor Garcia_28-May-13 4:59 
QuestionNice Pin
binglongwang5-Jul-12 15:48
binglongwang5-Jul-12 15:48 
QuestionReally helpful.. Pin
Anky Gupta1-Sep-11 19:03
Anky Gupta1-Sep-11 19:03 
GeneralVery Nice Pin
Member 36263802-Mar-11 6:05
Member 36263802-Mar-11 6:05 
GeneralExactly what I was looking for Pin
terpy26-Jun-09 15:59
terpy26-Jun-09 15:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.