Sign in to follow this  
Followers 0
aajames

How to sort an Array of numbers

5 posts in this topic

#1 ·  Posted (edited)

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

QUESTION #1- solved (read above)

Hello,

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

lets say i have an array of numbers such as:

Dim $avArray[4]

$avArray[0] = 4

$avArray[1] = 22

$avArray[2] = 19

$avArray[3] = 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[3] = 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[4]

$avArray[0] = 7

$avArray[1] = 12

$avArray[2] = 3

$avArray[3] = 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 this post


Link to post
Share on other sites



Dim $avArray[4]

$avArray[0] = "7"

$avArray[1] = "12"

$avArray[2] = "3"

$avArray[3] = "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[0] 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[0]
   $a[$i] = Number($a[$i])
Next


While 1
  $swap = 0
  For $i = 1 to $a[0] - 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[0]
   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 this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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[10]
$avArray[0] = 192
$avArray[1] = 128
$avArray[2] = 320
$avArray[3] = 174
$avArray[4] = 64
$avArray[5] = 32
$avArray[6] = 392
$avArray[7] = 1
$avArray[8] = 12
$avArray[9] = 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 this post


Link to post
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.

<{POST_SNAPBACK}>

Binary search works only correct on a sorted array!! :)

Parameters

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


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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[0] = 4
$avArray[1] = 22
$avArray[2] = 19
$avArray[3] = 6

; is same as...

$avArray[0] = " 4"
$avArray[1] = "22"
$avArray[2] = "19"
$avArray[3] = " 6"

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

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  
Followers 0