彰化一整天的論壇

 找回密碼
 立即註冊
查看: 111|回復: 3

【轉貼】字典排序(Sort a Dictionary by Key)

[複製鏈接]
發表於 2021-3-15 16:08:08 | 顯示全部樓層 |閱讀模式
  1. 'Force the explicit declaration of variables
  2. Option Explicit

  3. Sub SortDictionaryByKey()

  4.     'Set a reference to Microsoft Scripting Runtime by using
  5.     'Tools > References in the Visual Basic Editor (Alt+F11)

  6.     'Declare the variables
  7.     Dim Dict As Scripting.Dictionary
  8.     Dim TempDict As Scripting.Dictionary
  9.     Dim KeyVal As Variant
  10.     Dim Arr() As Variant
  11.     Dim Temp As Variant
  12.     Dim Txt As String
  13.     Dim i As Long
  14.     Dim j As Long
  15.    
  16.     'Create an instance of the Dictionary
  17.     Set Dict = New Dictionary
  18.    
  19.     'Set the comparison mode to perform a textual comparison
  20.     Dict.CompareMode = TextCompare
  21.    
  22.     'Add keys and items to the Dictionary
  23.     Dict.Add "Mango", "M250"
  24.     Dict.Add "Kiwi", "K150"
  25.     Dict.Add "Apple", "A325"
  26.     Dict.Add "Peach", "P350"
  27.     Dict.Add "Lime", "L275"
  28.    
  29.     'Allocate storage space for the dynamic array
  30.     ReDim Arr(0 To Dict.Count - 1)
  31.    
  32.     'Fill the array with the keys from the Dictionary
  33.     For i = 0 To Dict.Count - 1
  34.         Arr(i) = Dict.Keys(i)
  35.     Next i
  36.    
  37.     'Sort the array using the bubble sort method
  38.     For i = LBound(Arr) To UBound(Arr) - 1
  39.         For j = i + 1 To UBound(Arr)
  40.             If Arr(i) > Arr(j) Then
  41.                 Temp = Arr(j)
  42.                 Arr(j) = Arr(i)
  43.                 Arr(i) = Temp
  44.             End If
  45.         Next j
  46.     Next i
  47.    
  48.     'Create an instance of the temporary Dictionary
  49.     Set TempDict = New Dictionary
  50.    
  51.     'Add the keys and items to the temporary Dictionary,
  52.     'using the sorted keys from the array
  53.     For i = LBound(Arr) To UBound(Arr)
  54.         KeyVal = Arr(i)
  55.         TempDict.Add Key:=KeyVal, Item:=Dict.Item(KeyVal)
  56.     Next i
  57.    
  58.     'Set the Dict object to the TempDict object
  59.     Set Dict = TempDict
  60.    
  61.     'Build a list of keys and items from the original Dictionary
  62.     For i = 0 To Dict.Count - 1
  63.         Txt = Txt & Dict.Keys(i) & vbTab & Dict.Items(i) & vbCrLf
  64.     Next i
  65.    
  66.     'Display the list in a message box
  67.     MsgBox Txt, vbInformation
  68.    
  69. End Sub
複製代碼
文章來源: http://www.xl-central.com/sort-a-dictionary-by-key.html
回復

使用道具 舉報

發表於 2021-3-15 20:53:05 | 顯示全部樓層

那可以參考我改寫過的.
將資料讀進字典、排序、轉置
http://discuz.bestdaylong.com/thread-39231-1-1.html
(出處: 彰化一整天的論壇)
回復 支持 反對

使用道具 舉報

發表於 2021-3-15 21:22:26 | 顯示全部樓層
一樣很複雜丫,資質太差
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

 ㄚ母滴雞湯
 員林香純滴雞精

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)

GMT+8, 2021-4-11 02:49 , Processed in 0.106695 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回復 返回頂部 返回列表