Click here to Skip to main content
Licence CPOL
First Posted 5 Jun 2009
Views 13,858
Bookmarked 9 times

Sorting Hashtable

By | 5 Jun 2009 | Article
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).

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:

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)

About the Author

Igor Krupitsky

Web Developer

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionReally helpful.. Pinmembercoolguy_anky150519:03 1 Sep '11  
GeneralVery Nice PinmemberMember 36263806:05 2 Mar '11  
GeneralExactly what I was looking for Pinmemberterpy15:59 26 Jun '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Jun 2009
Article Copyright 2009 by Igor Krupitsky
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid