Announcement

Collapse
No announcement yet.

do you have a vbscript code snippet for sorting a collection?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    do you have a vbscript code snippet for sorting a collection?

    Do you have a vbscript code snippet for sorting a collection that you can share?

    thanks,

    Ken

    #2
    ' Stick your own, datatype-specific comparison operator where it belongs!
    ' stolen from various places, I certainly didn't originate QuickSort!

    Sub QuickSort(TheArray, LowerBound, UpperBound)
    Dim LowSwap, HighSwap
    Dim Pivot, Temp

    ' only 2 elements left to sort
    If UpperBound - LowerBound = 1 Then
    If TheArray(LowerBound) > TheArray(UpperBound) Then
    Temp = TheArray(LowerBound) 'swap 'em
    TheArray(LowerBound) = TheArray(UpperBound)
    TheArray(UpperBound) = Temp
    End If
    End If

    ' three elements to sort
    ' divide and conquer
    Pivot = TheArray(Int((LowerBound + UpperBound) / 2))
    TheArray(Int((LowerBound + UpperBound) / 2)) = TheArray(LowerBound)
    TheArray(LowerBound) = Pivot
    LowSwap = LowerBound + 1
    HighSwap = UpperBound
    Do ' Loop While LowSwap < HighSwap
    ' find the right low entry
    While LowSwap < HighSwap And TheArray(LowSwap) <= Pivot
    LowSwap = LowSwap + 1
    Wend
    '== Find the right HighSwap
    while TheArray(HighSwap) > Pivot
    HighSwap = HighSwap - 1
    wend
    '== Swap values if LowSwap is less then HighSwap
    if LowSwap < HighSwap then
    Temp = TheArray(LowSwap) 'swap 'em
    TheArray(LowSwap) = TheArray(HighSwap)
    TheArray(HighSwap) = Temp
    End If
    Loop While LowSwap < HighSwap

    TheArray(LowerBound) = TheArray(HighSwap)
    TheArray(HighSwap) = Pivot

    ' call this method recursively
    ' at least 2 elements in the first partition
    If LowerBound < (HighSwap - 1) Then
    Call QuickSort(TheArray, LowerBound, HighSwap - 1)
    End If
    ' at least 2 elements in the second partition
    If HighSwap + 1 < UpperBound Then
    Call QuickSort(TheArray, HighSwap + 1, UpperBound)

    End Sub 'QuickSort

    Comment


      #3
      Yikes. This message board apparently swallows leading white space...that code *was* nicely indented and easier to follow...sorry!

      Comment


        #4
        Then it won't eat anything. (See the "Instant UBBCode" buttons just below the message writing box to see what kinds of codes you can start with.)

        Nucleus Home Automation
        News, support, and updates for Rover, Network Monitor, TimeIcons, and more

        Comment


          #5
          thanks for the code.

          Comment


            #6
            I found some code that has several sorting functions.
            QSort - Allows sorting a 1 dimensional array.
            QSort2 - Sorts a 2 dimensional array on any column.
            Swap - Swaps two rows in a 2 dimensional array
            Transpose - Changes a 2 dimensional array from array(x,y) to array(y,x)
            RSort - Reverses an array.

            If you want the file that has these functions go to:
            http://www.c-website.com/homeseer/default.cfm?tab=8
            Download devicelist.zip and just extract the file QSort.asp

            Jeff Farmer

            --
            Jeff Farmer
            HS 3, HSPhone
            My HS3 Plugins: CFHSExtras, Random, Restart, Tracker, WeatherXML, PanaBluRay
            Other Plugins In Use: APCUPSD, BLOnkyo, Device History, EasyTrigger, HSTouch Server, PHLocation2, Pushover, RFXCom, UltraGCIR3, UltraMon3, UltraPioneerAVR3, X10, Z-Wave

            Hardware: GoControl Irrigation Controler, Schlage Lever Lock, Schlage Deadbolt, Way2Call Hi-Phone, RFXCom RFXrec433 Receiver, WGL 800, TI-103, Z-Net, Pioneer 1120, Pioneer 1021, Pioneer LX302, Panasonic BDT-110, Panasonic BDT-210 x2

            Comment

            Working...
            X