Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: SortArray() - Sort single dimension array[Next]: TimeStamp() - including files
\->VBSCRIPT Files->SortDictionary() - Sorts dictionary objects

[anchor]

The Code for: "SortDictionary() [Sorts dictionary objects].vbs.txt"

This is the example, a shortcut was installed to this code :

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[vbs].SortDictionary() [Sorts dictionary objects].vbs.txt.pvcs   1.0   29 Jun 2014 12:51:22   USER "Dennis"  $
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


const SortByDictionaryKey            = 1
const SortByDictionaryValue          = 2
dim CompareDictionaryCaseSensitive : CompareDictionaryCaseSensitive = vbBinaryCompare
dim CompareDictionaryCaseIgnored   : CompareDictionaryCaseIgnored   = vbTextCompare

'============================================================================
function SortDictionary(oDictionary, SortByKeyOrItem, HowToCompare)
'        http://support.microsoft.com/kb/246067
'        HowToCompare = vbTextCompare
'============================================================================
    dim strDict()
    dim objKey
    dim strKey,strItem
    dim X,Y,Z
    Z = oDictionary.Count
    if  Z > 1 Then
        ReDim strDict(Z,2)
        X = 0
        for Each objKey In oDictionary
            strDict(X,SortByDictionaryKey)   = CStr(objKey)
            strDict(X,SortByDictionaryValue) = CStr(oDictionary(objKey))
            X = X + 1
        next
        for X = 0 to (Z - 2)
            for Y = X to (Z - 1)
                if StrComp( strDict(X,SortByKeyOrItem), strDict(Y,SortByKeyOrItem), HowToCompare) > 0 Then
                    strKey  = strDict(X,SortByDictionaryKey)
                    strItem = strDict(X,SortByDictionaryValue)
                    strDict(X,SortByDictionaryKey)   = strDict(Y,SortByDictionaryKey)
                    strDict(X,SortByDictionaryValue) = strDict(Y,SortByDictionaryValue)
                    strDict(Y,SortByDictionaryKey)   = strKey
                    strDict(Y,SortByDictionaryValue) = strItem
                end if
            next
        next
        oDictionary.RemoveAll
        for X = 0 to (Z - 1)
            oDictionary.Add strDict(X,SortByDictionaryKey), strDict(X,SortByDictionaryValue)
        next
    end if
end function


'#####################################################################################
'#####################################################################################
'#####################################################################################


  Set d = wscript.CreateObject("Scripting.Dictionary")

  d.Add "3", "Delta"
  d.Add "1", "Foxtrot"
  d.Add "4", "Bravo"
  d.Add "2", "Echo"
  d.Add "6", "Alpha"
  d.Add "10", "GGGGGGGGGGGGG - Wont sort correct by KEY as text comparison"
  d.Add "5", "Charlie"

  wscript.echo "<p>Before Sorting:<br>"
  for Each i In d
    wscript.echo i & "=" & d(i) & "<br>"
  next

  wscript.echo vbCRLF & "<p>By Key:<br>"
  SortDictionary d, SortByDictionaryKey, CompareDictionaryCaseIgnored
  for Each i In d
    wscript.echo i & "=" & d(i) & "<br>"
  next

  wscript.echo vbCRLF & "<p>By Item:<br>"
  SortDictionary d,SortByDictionaryValue, CompareDictionaryCaseIgnored
  for Each i In d
    wscript.echo d(i) & "=" & i & "<br>"
  next


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: SortArray() - Sort single dimension array[Next]: TimeStamp() - including files


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.