# How to sort an Array of numbers

## Recommended Posts

EDIT#3: ok i found the problem with the first question.

In order to sort an array of numbers, or to do an alphanumerical sort, they must be NUMBERS...duh... when adding them to the array i simply used the Number() to convert them from string and it worked. Now i just need to get the 2nd question working

Hello,

Yep, im a newbie... and here is a newbie question.

lets say i have an array of numbers such as:

Dim \$avArray

\$avArray = 4

\$avArray = 22

\$avArray = 19

\$avArray = 6

Then lets say I wanted to SORT them in order of lowest to highest.... where lets say the array would looks something like this:

\$avArray = [4,6,19,22]

And so the following would be true:

\$avArray = 22

How do i do this? I tried _ArraySort and it seems to only work on strings.

Seems like this is a pretty basic request and I cant imagine there isnt an easy answer...but I can not for the life of me find that easy answer.

thanks much and have a great weekend,

chris

QUESTION #2

Here is another problem i am having.

Lets say i have another small array of numbers such as:

Dim \$avArray

\$avArray = 7

\$avArray = 12

\$avArray = 3

\$avArray = 5

Then lets say I would like to see if a number 12 is in the array:

\$xLoc = _ArrayBinarySearch( \$avArray, 12 )

The results i get back are that it is NOT in the array... Error 3 Does _ArrayBinarySearch only work on strings? Or am i missing something here.

Edited by aajames
##### Share on other sites

Dim \$avArray

\$avArray = "7"

\$avArray = "12"

\$avArray = "3"

\$avArray = "5"

you have defined the numbers as strings - w/o looking at the udf you've appropriated for your use, i would expect it to sort in the order of 12,3,5,7.

When using a UDF you did not write, it is always a good idea to look over the code and ensure that you know what it is designed to do.

Define your values as numbers, (no quotes) and use mathmatical operators to compare them.. Au3 is not a rigorous type language, (essentially everything is rendered internally as a variant) , thus issues like this can arise. You can use the Is* functions to determine whether the contents of the variable will successfully typecast to the desired type.

A practice I use is to use the \$a element to store the number of values in the array \$a , yielding:

```\$a = StringSplit("1,2,5,4,76,78,34,52",",")
For \$i = 1 to \$a
\$a[\$i] = Number(\$a[\$i])
Next

While 1
\$swap = 0
For \$i = 1 to \$a - 1
if \$a[\$i] > \$a[\$i + 1] then
\$hi = \$a[\$i]
\$a[\$i] = \$a[\$i+1]
\$a[\$i+1] = \$hi
\$swap = 1
EndIf
Next
if \$swap = 0 then ExitLoop
Wend

For \$i = 1 to \$a
ConsoleWrite(\$a[\$i] & @LF)
Next```

Any Programming 101 course will contain multiplicities of faster sorts than this

bubble...pick one and implement it so you know it works for what you want.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

##### Share on other sites

Thanks for the reply... its much appreciated.

As for not getting _ArrayBinarySearch to work with an array of numbers, i simply just wrote my own that seems to work fine.

```#include <Array.au3>

Dim \$avArray
\$avArray = 192
\$avArray = 128
\$avArray = 320
\$avArray = 174
\$avArray = 64
\$avArray = 32
\$avArray = 392
\$avArray = 1
\$avArray = 12
\$avArray = 123

\$xtemp = _GetPos( \$avArray , 128 )
MsgBox(0,"", \$xtemp ,2)

Func _GetPos( \$avArray, \$xNum )
;Find and see if \$xNum is in array \$avArray, if it is return 1st loc, if not, return -1
For \$i = 0 To ( UBound( \$avArray ) - 1 )
if \$xNum = \$avArray[\$i] then
Return \$i
EndIf
Next
Return -1
EndFunc```
Edited by aajames
##### Share on other sites
• Developers

Thanks for the reply... its much appreciated.

As for not getting _ArrayBinarySearch to work with an array of numbers, i simply just wrote my own that seems to work fine.

<{POST_SNAPBACK}>

Binary search works only correct on a sorted array!! Parameters

\$avArray The 1-dimensional sorted array to search on.

Live for the present,
Dream of the future,
Learn from the past. ##### Share on other sites

If the numbers are encased in qoutation marks, so it is recognised as a string, you can still sort them correctly but all 'string numbers' must have the same length.

```\$avArray = 4
\$avArray = 22
\$avArray = 19
\$avArray = 6

; is same as...

\$avArray = " 4"
\$avArray = "22"
\$avArray = "19"
\$avArray = " 6"```

qq

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...