Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. Function DictToSortedArray(D As Object) As Variant
  2. 'returns a 1-based 2-dimensional sorted array
  3. 'sorted by the keys
  4. Dim A As Variant, i As Long, AL As Object, k As Variant
  5.  
  6. Set AL = CreateObject("System.Collections.ArrayList")
  7.  
  8. For Each k In D
  9. AL.Add k
  10. Next k
  11.  
  12. AL.Sort
  13.  
  14. ReDim A(1 To AL.Count, 1 To 2)
  15.  
  16. For i = 1 To AL.Count
  17. A(i, 1) = AL(i - 1)
  18. A(i, 2) = D(AL(i - 1))
  19. Next i
  20.  
  21. DictToSortedArray = A
  22. End Function
  23.  
  24. Sub test()
  25. Dim D As Object
  26. Dim A As Variant
  27. Dim i As Long
  28.  
  29. Set D = CreateObject("Scripting.Dictionary")
  30. D.Add 5, 8
  31. D.Add 3, 7
  32. D.Add 42, 9
  33. D.Add 1, 7
  34. D.Add 10, 11
  35.  
  36. A = DictToSortedArray(D)
  37. For i = 1 To 5
  38. Debug.Print A(i, 1) & ", " & A(i, 2)
  39. Next i
  40. End Sub
  41.  
  42. 1, 7
  43. 3, 7
  44. 5, 8
  45. 10, 11
  46. 42, 9
  47.  
  48. Sub Tester()
  49.  
  50. Dim d As Object
  51. Dim i As Long, arr, k
  52.  
  53. Set d = CreateObject("scripting.dictionary")
  54.  
  55.  
  56. With d
  57. .Add 3, 33
  58. .Add 1, 33
  59. .Add 2, 55
  60. .Add 5, 77
  61. End With
  62.  
  63. arr = d.keys '<< get keys in an array
  64.  
  65. ' "sort" through the array, and get the values from the dictionary
  66. Debug.Print "key", "value"
  67. For i = 0 To UBound(arr)
  68. k = Application.Small(arr, i + 1)
  69. Debug.Print k, d(k)
  70. Next i
  71.  
  72. End Sub
  73.  
  74. key value
  75. 1 33
  76. 2 55
  77. 3 33
  78. 5 77
  79.  
  80. Option Explicit
  81.  
  82. Sub sortedDictionary()
  83. Dim i As Long, j As Long, d As Long, dict As Object
  84. Dim vKEYs As Variant, tmp As Variant
  85.  
  86. Set dict = CreateObject("Scripting.Dictionary")
  87.  
  88. With Worksheets("Sheet4")
  89. For d = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
  90. dict.Item(.Cells(d, "A").Value2) = .Cells(d, "B").Value2
  91. Next d
  92.  
  93. vKEYs = dict.keys
  94.  
  95. For i = LBound(vKEYs) + 1 To UBound(vKEYs)
  96. For j = LBound(vKEYs) To UBound(vKEYs) - 1
  97. If vKEYs(j) > vKEYs(i) Then
  98. tmp = vKEYs(j)
  99. vKEYs(j) = vKEYs(i)
  100. vKEYs(i) = tmp
  101. End If
  102. Next j
  103. Next i
  104.  
  105. ReDim tmp(1 To UBound(vKEYs) + 1, 1 To 2)
  106.  
  107. For i = LBound(vKEYs) To UBound(vKEYs)
  108. tmp(i + 1, 1) = vKEYs(i)
  109. tmp(i + 1, 2) = dict.Item(vKEYs(i))
  110. Next i
  111.  
  112. .Cells(2, "E").Resize(UBound(tmp, 1), UBound(tmp, 2)) = tmp
  113. End With
  114. End Sub
  115.  
  116. Sub SortDictionary(dict As Object)
  117. Dim i As Long
  118. Dim key As Variant
  119.  
  120. With CreateObject("System.Collections.SortedList")
  121. For Each key In dict
  122. .Add key, dict(key)
  123. Next
  124. dict.RemoveAll
  125. For i = 0 To .Keys.Count - 1
  126. dict.Add .GetKey(i), .Item(.GetKey(i))
  127. Next
  128. End With
  129. End Sub
  130.  
  131. SortDictionary dict '<--| give 'SortDictionary()' sub a dictionary object to sort by its keys
  132.  
  133. Sub main()
  134.  
  135. Dim dict As Object
  136. Dim key As Variant
  137.  
  138. Set dict = CreateObject("Scripting.Dictionary")
  139. With dict
  140. .Add 5, 15
  141. .Add 4, 14
  142. .Add 3, 13
  143. .Add 2, 12
  144. .Add 1, 11
  145. End With
  146.  
  147. SortDictionary dict
  148.  
  149. With dict
  150. For Each key In .Keys
  151. Debug.Print key, .Item(key)
  152. Next
  153. End With
  154. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement