Jump to content
Sign in to follow this  
_Kurt

Arrange Arrays Alphanumerically

Recommended Posts

Made a small, simple, easy to use function that will arrange your arrays alphanumerically. I'm not sure if anyone had made something like this before, but by searching the forum I had not seen something like this.

Example:

#include <Array.au3> ;Used only for _Arraydisplay

Local $Array[11]
$Array[0] = "5 is number five"
$Array[1] = ""
$Array[2] = "2"
$Array[3] = "1 I'm #1 :)"
$Array[4] = "4"
$Array[5] = "3.. Blah"
$Array[6] = "D-erek"
$Array[7] = "A-lex"
$Array[8] = "E-ric"
$Array[9] = "C-hris"
$Array[10] = "B-ob"
$NewArray = _ArrayArrangeAlNum($Array)
_ArrayDisplay($NewArray, "$NewArray")oÝ÷ Ù8^éܶ*'jëh×6#include <GUIList.au3>

Func _ArrayArrangeAlNum($Array)
    If NOT IsArray($Array) Then Return 0
    Local $Str = "", $NewArray[UBound($Array)], $Num = 0
    For $i = 0 To UBound($Array)-1
        If $Array[$i] <> "" Then
            $Str &=  "|" & $Array[$i]
        Else
            $NewArray[$Num] = ""
            $Num = $Num+1
        EndIf
    Next
    $A = GUICreate("")
    $B = GuiCtrlCreateList("", 0, 0, 300, 300, 0x0002)
    GUICtrlSetData(-1,$Str)
    For $i = $Num To UBound($Array)-1
        $NewArray[$i] = _GUICtrlListGetText($B, $i-$Num)
    Next
    GUIDelete($A)
    Return $NewArray
EndFunc

EDIT: Also forgot to mention, blank strings ( $array[x] = "" ) will be put at the start of the array.

Kurt

Edited by _Kurt

Share this post


Link to post
Share on other sites

#include<Array.au3>
Dim $arrayz[7]
$arrayz[0]="0"
$arrayz[1]=""
$arrayz[2]="14"
$arrayz[3]="asd"
$arrayz[4]="nvc"
$arrayz[5]="A34"
$arrayz[6]="f4"


order($arrayz)
_ArrayDisplay($arrayz)

Func order(ByRef $array)
    Do
        $s=0
        For $i=0 To UBound($array)-2
            If $array[$i]>$array[$i+1] Then
                $temp=$array[$i]
                $array[$i]=$array[$i+1]
                $array[$i+1]=$temp
                $s=1
            EndIf
        Next
    Until $s=0
EndFunc


Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites
:) ... what is the difference with this and _ArraySort() ? Edited by JdeB

Share this post


Link to post
Share on other sites

I'd place my bet on it being slower -- much slower. There's too much overhead in working with a list control just for sorting.

#include<Array.au3>
Dim $array[101][2]

For $i=0 To 100
Dim $arrayz[7]
$arrayz[0]="0"
$arrayz[1]=""
$arrayz[2]="14"
$arrayz[3]="asd"
$arrayz[4]="nvc"
$arrayz[5]="A34"
$arrayz[6]="f4"

$asd=TimerInit()
order($arrayz)
$array[$i][0]=TimerDiff($asd)

$arrayz[0]="0"
$arrayz[1]=""
$arrayz[2]="14"
$arrayz[3]="asd"
$arrayz[4]="nvc"
$arrayz[5]="A34"
$arrayz[6]="f4"
$asd2=TimerInit()
_ArraySort($arrayz)
$array[$i][1]=TimerDiff($asd2)
Next
_ArrayDisplay($array)
Func order(ByRef $array)
    Do
        $s=0
        For $i=0 To UBound($array)-2
            If $array[$i]>$array[$i+1] Then
                $temp=$array[$i]
                $array[$i]=$array[$i+1]
                $array[$i+1]=$temp
                $s=1
            EndIf
        Next
    Until $s=0
EndFuncoÝ÷ Úȳz¬¥©ì·)^
Edited by alexmadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites

I'm not sure what you're getting all worked up about. It looked to me like JdeB was asking what the difference was between _Kurt's function and _ArraySort(), and I said it was slower. I never even looked at your function. I thought that was pretty obvious, considering the fact that I mentioned lists in the first place.

Edited by -Ultima-

Share this post


Link to post
Share on other sites

now try something more substantial to sort... like this :)

#include<Array.au3>
Dim $array[101][2]
For $i = 0 To 20
    Dim $array1[100],$array2[100]
    For $x = 0 to UBound($array1)-1
        $array1[$x] = Random(1,99999999)
        $array2[$x] = $array1[$x]
    Next
    $asd = TimerInit()
    order($array1)
    $array[$i][0] = TimerDiff($asd)
    $asd2 = TimerInit()
    _ArraySort($array2)
    $array[$i][1] = TimerDiff($asd2)
Next
_ArrayDisplay($array)
Func order(ByRef $array)
    Do
        $s = 0
        For $i = 0 To UBound($array) - 2
            If $array[$i] > $array[$i + 1] Then
                $temp = $array[$i]
                $array[$i] = $array[$i + 1]
                $array[$i + 1] = $temp
                $s = 1
            EndIf
        Next
    Until $s = 0
EndFunc  ;==>order

Share this post


Link to post
Share on other sites

Hi,

jDeb is opinting out that larger arrays will benefit from the quicksort algorithm in ArraySort... immensely.. - here's 199 random elements; try 2000 and you'll spend a long time..

Best, randall

[0]|304.670732074067|31.4530001861691

[1]|308.610872273697|36.5806237379885

[2]|364.124882570279|33.6781960217102

[3]|286.373674977445|31.4495592805281

[4]|303.292049148635|35.1598755531369

[5]|295.63468329777|34.7648255022985

Share this post


Link to post
Share on other sites

that's a very normal thing.... my func is for small things....

as you can see ... it will pass through the whole array lots of times...(btw i learnt that func in school :))

Edited by alexmadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites

that's a very normal thing.... my func is for small things....

Not sure where small ends and large starts but running the test you submitted it takes your bubblesort 0.00174 secs and _ArraySort() 0.00264 sec on my PC per array sort operation.

I would use the supplied UDF anyways for Small arrays ... :)

Edited by JdeB

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...