Click here to Skip to main content
Click here to Skip to main content

Tagged as

Quick And Dirty Dictionary Sort on Integer Value

, 18 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The Dictionary class does not have inbuilt sort functionality and I was looking for a simple way to sort on the Values within the collection.After searching google, it became clear that there are several ways to achieve this, from implementing Lists, using LookUps and even making use of...
The Dictionary class does not have inbuilt sort functionality and I was looking for a simple way to sort on the Values within the collection.
 
After searching google, it became clear that there are several ways to achieve this, from implementing Lists, using LookUps and even making use of LINQ. I just wanted something quick and dirty that gets the job done. Wasn't concerned with overheads or speed of execution. There is even an Article here on codeproject A Dictionary Collection Sorting By Value[^] which as you can see is way in excess of my simple function. However, if you are looking for a professional way to do it, then it maybe better to implement something like that.
 
My dictionary consisted of a String Key, Integer Value pair where the String Key represent an equipment tagname, and the Integer Value was the number of records that were present in the alarm / event logs (the returned database records), e.g.
"TagDEF", 364
"TagXYZ", 2102
"TagABC", 56
"TagMNO", 100
"TagJKL", 10
 
So I knocked up this simple function, where the dictionary is passed in, and a new value sorted dictionary is returned. Now, don't get me wrong, my version is maybe just a dirty hack, but it works and gets the job done. It is simple to follow [good for non-pro's like me], although it maybe isn't the worlds fastest function, it gets the job done.
 
Going, by the number of questions I have seen on the net, I thought I would share it.
 
    Private Function DictValueSortDesc(ByVal SourceDictionary As Dictionary(Of String, Integer)) As Dictionary(Of String, Integer)
 
        'Do a Dictionary Sort on the Value Highest to lowest
        Dim inDict As Dictionary(Of String, Integer) = SourceDictionary
        Dim outDict As New Dictionary(Of String, Integer)
        Dim currentMaxKey As String = String.Empty
        Dim currentMaxValue As Integer = 0
 
        Do While inDict.Keys.Count > 0
            For Each currentKVP As KeyValuePair(Of String, Integer) In inDict
                If currentKVP.Value >= currentMaxValue Then
                    currentMaxKey = currentKVP.Key
                    currentMaxValue = currentKVP.Value
                End If
            Next
 
            'Add the new current iteration Key/Value to the new dictionary and remove from old
            outDict.Add(currentMaxKey, currentMaxValue)
 
            'remove the old item
            inDict.Remove(currentMaxKey)
            currentMaxValue = 0
        Loop
 
        Return outDict
 
    End Function
 
So for the Dictionary example above, the output dictionary would look like;
"TagXYZ", 2102
"TagDEF", 364
"TagMNO", 100
"TagABC", 56
"TagJKL", 10

License

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

Share

About the Author

DaveAuld
Engineer
Scotland Scotland
I have been working in the Oil & Gas Industry for over 25 years now.
 
Core Discipline is Instrumentation and Control Systems.
 
Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.
 
Currently, Offshore Installation Manager for the Beryl Bravo platform, which is located ~180 miles NE of Aberdeen, Scotland in the Northern North Sea.
Formely on the Forties Charlie platform, which is located ~110Miles NE of Aberdeen.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 5 Clean easy to understand PinmemberSimon_Whale30-Jun-10 4:38 
Reason for my vote of 5
Clean easy to understand

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141030.1 | Last Updated 18 Jun 2010
Article Copyright 2010 by DaveAuld
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid