Jump to content

_ArraySort_ByDelimitedItem UDF


andybiochem
 Share

Recommended Posts

Hi,

Just thought I'd share this - I use it quite a lot, thought it might be useful to someone.....nothing special really.

What it does:

Say you had an array of data looking like this:

12,123,33

23,43,123

45,55,323

3,443,4

74,46,88

99,102,9

....and you wanted to sort it low to high by reference to the 3rd item, to look like this:

3,443,4

99,102,9

12,123,33

74,46,88

23,43,123

45,55,323

You can use this UDF to do that.

UDF:

; #FUNCTION# ============================================================================
; Name...........: _ArraySort_ByDelimitedItem
; Description ...: Sorts delimited data by item reference
; Syntax.........: _ArraySort_ByDelimitedItem(ByRef $aArray,$cDelimiter,$iItem,$iDescending = 0)
; Parameters ....:  $aArray - Array of delimited data to sort
;                   $cDelimiter - delimiter character
;                   $iItem - data item to sort by
;                   $iDescending - 0=low to high, 1=high to low
; Notes..........: Requires <Array.au3>
; =======================================================================================
Func _ArraySort_ByDelimitedItem(ByRef $aArray,$cDelimiter,$iItem,$iDescending = 0)
    Local $aItems[UBound($aArray)][2]
    For $i = 1 To (UBound($aItems) - 1)
        $aItems[$i][0] = $aArray[$i]
        $aData = StringSplit($aArray[$i],$cDelimiter)
        $aItems[$i][1] = Number($aData[$iItem])
    Next
    _ArraySort($aItems,$iDescending,1,UBound($aItems) - 1,1)
    For $i = 1 to (UBound($aArray) - 1)
        $aArray[$i] = $aItems[$i][0]
    Next
EndFunc

Example useage:

#include <Array.au3>

;----- create some random data to sort -----
Dim $Data[101]
For $i = 1 to 100
    $Data[$i] = Random(0,100,1) & "," & Random(0,100,1) & "," & Random(0,100,1)
Next

_ArrayDisplay($Data,"Data NOT sorted")

_ArraySort_ByDelimitedItem($Data,",",3,0)

_ArrayDisplay($Data,"Data sorted by 3rd item")



; #FUNCTION# ============================================================================
; Name...........: _ArraySort_ByDelimitedItem
; Description ...: Sorts delimited data by item reference
; Syntax.........: _ArraySort_ByDelimitedItem(ByRef $aArray,$cDelimiter,$iItem,$iDescending = 0)
; Parameters ....:  $aArray - Array of delimited data to sort
;                   $cDelimiter - delimiter character
;                   $iItem - data item to sort by
;                   $iDescending - 0=low to high, 1=high to low
; Notes..........: Requires <Array.au3>
; =======================================================================================
Func _ArraySort_ByDelimitedItem(ByRef $aArray,$cDelimiter,$iItem,$iDescending = 0)
    Local $aItems[UBound($aArray)][2]
    For $i = 1 To (UBound($aItems) - 1)
        $aItems[$i][0] = $aArray[$i]
        $aData = StringSplit($aArray[$i],$cDelimiter)
        $aItems[$i][1] = Number($aData[$iItem])
    Next
    _ArraySort($aItems,$iDescending,1,UBound($aItems) - 1,1)
    For $i = 1 to (UBound($aArray) - 1)
        $aArray[$i] = $aItems[$i][0]
    Next
EndFunc

Like I said, nothing much really, just makes sorting CSVs and the like a bit easier.

Ta!

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...