Click here to Skip to main content
12,755,775 members (39,228 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

35.8K views
9 bookmarked
Posted 5 Jun 2009

Sorting Hashtable

, 5 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Igor Krupitsky
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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
VitorHugoGarcia28-May-13 5:59
memberVitorHugoGarcia28-May-13 5:59 
QuestionNice Pin
binglongwang5-Jul-12 16:48
memberbinglongwang5-Jul-12 16:48 
QuestionReally helpful.. Pin
coolguy_anky15051-Sep-11 20:03
membercoolguy_anky15051-Sep-11 20:03 
GeneralVery Nice Pin
Member 36263802-Mar-11 7:05
memberMember 36263802-Mar-11 7:05 
GeneralExactly what I was looking for Pin
terpy26-Jun-09 16:59
memberterpy26-Jun-09 16: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.

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