Every day you wake up is a victory. Learn more and share more….

Concept 1:-How to find the occurrence count of item in a given sample?

Sorting using Dictionary object


Using dictionary object we would find the count of occurrence of given value in the sample.
Example: – we need to find the “Help” words in the given string say: – “God help those who help themselves”

Lets connect for more updates at  Fb,G+,Twitter.
Code:-

'Input String
sSample = "God help those who help themselves"
'Splitting the given string
arrSample = Split(sSample)
'Calling the function to get the Count of all the items
Set objCount = fn_Count(arrSample)
arrKey = objCount.Keys
For iRun = 0 To objCount.Count - 1
MsgBox "Count of '" & arrKey(iRun) & "' =" & objCount.Item(arrKey(iRun))
Next
'*******************************************Function*********************************
'Function Name:- fn_Count
'Function Description:- Function find the occuerence of given word in the string
'Input Parameters:- Given String
'Output Parameters:- Dictionary object
'************************************************************************************

Function fn_Count(arrSample)
Dim iRow, iItem
Dim objDic
Set objDic = CreateObject("scripting.dictionary")
'Looping through array
For iRow = LBound(arrSample) To UBound(arrSample)
iItem = arrSample(iRow)
If Not objDic.Exists(iItem) Then
objDic.Add iItem, 1
Else
objDic.Item(iItem) = objDic.Item(iItem) + 1
End If
Next

Set fn_Count = objDic
End Function
Concept 2:- How to sort items in the given dictionary?
Note: – we can sort dictionary keys in the same manner as we did items.

Code:-
'*******************************************Function*********************************
'Function Name:- fn_SortDictionary
'Function Description:- Function to sort the items in the given dictionary
'Input Parameters:- Unsorted Dictionary object
'Output Parameters:- Sorted Dictionary object
'************************************************************************************
Function fn_SortDictionary(objdict)
Dim i, j, temp
For Each i In objdict
For Each j In objdict
If (objdict.Item(i) <= objdict.Item(j)) Then
temp = objdict.Item(i)
objdict.Item(i) = objdict.Item(j)
objdict.Item(j) = temp
End If
Next
Next
Set fn_SortDictionary = objdict
End Function
Concept 3:- How to sort dictionary Key-Item pairs based on the Item or key?

This is different from the above concept as we are not simply sorting the items or keys present in the dictionary but actually we are creating a new sorted dictionary. Some time it is required to sort the Key-Value pairs in ascending or descending order.
Note: – Here we are sorting on the basis of items and some time our item values can be repeating.

Code:-
    'Creating dictionary
Dim objDic
Set objDic = CreateObject("scripting.dictionary")
objDic.Add "USD", "80"
objDic.Add "SAG", "55"
objDic.Add "CAD", "80"
objDic.Add "AUD", "50"

'Function call to sort the dictionary
Set objSDic = fn_SortDictionary(objDic)
'Printing the Result of sorted Dictionary in the Msgbox
For i = 0 To objSDic.Count - 1
Txt = Txt & objSDic.keys()(i) & objSDic.items()(i) & vbCrLf
Next i
MsgBox Txt




'*******************************************Function*********************************
'Function Name:- fn_SortDictionary
'Function Description:- Function to sort the Key and item pairs in the given dictionary
'Input Parameters:- Unsorted Dictionary object
'Output Parameters:- Sorted Dictionary object
'************************************************************************************
Function fn_SortDictionary(objIDict)

'Taking the Orginal Keys
arrKey = objIDict.keys
'Taking the count of items values
arrSort = objIDict.items

'We would sort on the basis of Items but for keeping track which item belongs to the key
'we would add index value to the existing items
'Incase we have duplicating items we be still be able to sort.
'Adding index to all the items
For i = LBound(arrSort) To UBound(arrSort)
arrSort(i) = arrSort(i) & ";" & i
Next
'Logic for sorting

For i = LBound(arrSort) To UBound(arrSort) - 1
For j = i + 1 To UBound(arrSort)
'Split the value of arrsort so that we dont consider the index part
arrFSortI = Split(arrSort(i), ";")
arrFSortJ = Split(arrSort(j), ";")
If Cint(arrFSortI(0)) < Cint(arrFSortJ(0)) Then
'Concatenating the split array and joining the Index at the end
Temp = arrFSortJ(0) & ";" & arrFSortJ(1)
arrSort(j) = arrFSortI(0) & ";" & arrFSortI(1)
arrSort(i) = Temp
End If
Next j
Next i
'Creating the Temp Dictionary object
Set TempDict = CreateObject("scripting.Dictionary")
'Adding sorted values to the dictionary
For i = LBound(arrSort) To UBound(arrSort)
KeyIndex = Split(arrSort(i), ";")
TempDict.Add Key:=arrKey(KeyIndex(1)), Item:=KeyIndex(0)
Next i
'Returning the sorted dictionary
Set fn_SortDictionary = TempDict
End Function


Leave a Reply

Your email address will not be published. Required fields are marked *