Sort dictionary items

User avatar
sal21
PlatinumLounger
Posts: 4370
Joined: 26 Apr 2010, 17:36

Sort dictionary items

Post by sal21 »

From a loop in txt file i fill a Dictionary with:

Code: Select all

....
     Dim MYDIC As Object
    Set MYDIC = New Scripting.Dictionary

....

If Not MYDIC.Exists(COD) Then
            MYDIC.Add COD, DESCR_COD
End If
ho to order the items of MYDIC by Key COD?

note:
COD and DESCR_COD are string var

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: sort Dictionatry items

Post by HansV »

Add the following procedure:

Code: Select all

Sub SortDict(dic As Scripting.Dictionary)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim arr() As String
    Dim tmpdic As Scripting.Dictionary
    ' Get out if dictionary is empty
    If dic Is Nothing Then
        Exit Sub
    End If
    ' or if it has at most one item
    If dic.Count <= 1 Then
        Exit Sub
    End If
    ' Fill arrays with arr
    ReDim arr(0 To dic.Count - 1)
    For i = 0 To dic.Count - 1
        arr(i) = dic.Keys(i)
    Next i
    ' Sort the array
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    ' Fill new dictionary sorted by key
    Set tmpdic = New Scripting.Dictionary
    For i = 0 To dic.Count - 1
        tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i))
    Next i
    ' Set original dictionary to temp one
    Set dic = tmpdic
End Sub
To sort your dictionary by key, call

SortDict MYDIC
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4370
Joined: 26 Apr 2010, 17:36

Re: sort Dictionatry items

Post by sal21 »

HansV wrote:Add the following procedure:

Code: Select all

Sub SortDict(dic As Scripting.Dictionary)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim arr() As String
    Dim tmpdic As Scripting.Dictionary
    ' Get out if dictionary is empty
    If dic Is Nothing Then
        Exit Sub
    End If
    ' or if it has at most one item
    If dic.Count <= 1 Then
        Exit Sub
    End If
    ' Fill arrays with arr
    ReDim arr(0 To dic.Count - 1)
    For i = 0 To dic.Count - 1
        arr(i) = dic.Keys(i)
    Next i
    ' Sort the array
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    ' Fill new dictionary sorted by key
    Set tmpdic = New Scripting.Dictionary
    For i = 0 To dic.Count - 1
        tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i))
    Next i
    ' Set original dictionary to temp one
    Set dic = tmpdic
End Sub
To sort your dictionary by key, call

SortDict MYDIC
NOTE:
- "tmpdic" is not dimensiioned wath type?
- but is really required to fill, before all, a new array "arr"?

Work perfect in other case :thankyou: :clapping: :clapping:

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sort dictionary items

Post by HansV »

The line

Code: Select all

    Dim tmpdic As Scripting.Dictionary
declares tmpdic as a Dictionary.
I don't know of a way to sort a dictionary directly; the array is used to sort the keys.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4370
Joined: 26 Apr 2010, 17:36

Re: Sort dictionary items

Post by sal21 »

HansV wrote:The line

Code: Select all

    Dim tmpdic As Scripting.Dictionary
declares tmpdic as a Dictionary.
I don't know of a way to sort a dictionary directly; the array is used to sort the keys.
Good!

User avatar
sal21
PlatinumLounger
Posts: 4370
Joined: 26 Apr 2010, 17:36

Re: sort Dictionatry items

Post by sal21 »

HansV wrote:Add the following procedure:

Code: Select all

Sub SortDict(dic As Scripting.Dictionary)
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim arr() As String
    Dim tmpdic As Scripting.Dictionary
    ' Get out if dictionary is empty
    If dic Is Nothing Then
        Exit Sub
    End If
    ' or if it has at most one item
    If dic.Count <= 1 Then
        Exit Sub
    End If
    ' Fill arrays with arr
    ReDim arr(0 To dic.Count - 1)
    For i = 0 To dic.Count - 1
        arr(i) = dic.Keys(i)
    Next i
    ' Sort the array
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    ' Fill new dictionary sorted by key
    Set tmpdic = New Scripting.Dictionary
    For i = 0 To dic.Count - 1
        tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i))
    Next i
    ' Set original dictionary to temp one
    Set dic = tmpdic
End Sub
To sort your dictionary by key, call

SortDict MYDIC

Summ +1 for each Key found

My prob...

Is possibke to summ how many key/COD during the creation of the Dictionary Object?

example:

if not exists store COD+1

if exists add + 1 to the alreday COD+1 just stored in Dictionary Key

???'

in effect similar during the loop of txt file:
...
0900
0901
0900
....
and after reading txt file this result for me:

2-0900
1-0901

note:
sure with this new scenario to order the items based COD is different or not?

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sort dictionary items

Post by HansV »

That doesn't make sense - you shouldn't change the key of a dictionary item.
Best wishes,
Hans