Sign in to follow this  
Followers 0
brodie28

Sorting a two dimensional array

6 posts in this topic

I have an array set up like this

Dim $targets[4][unknown]

On the $targets[3][$i] in the array, a bunch of numbers are stored.

I want to sort the array so that it gets sorted with those numbers going from smallest to biggest.

I know it is done with _Arraysort, but I am not sure how to set it up so that it does what I want it to.

Share this post


Link to post
Share on other sites



I have an array set up like this

Dim $targets[4][unknown]

On the $targets[3][$i] in the array, a bunch of numbers are stored.

I want to sort the array so that it gets sorted with those numbers going from smallest to biggest.

I know it is done with _Arraysort, but I am not sure how to set it up so that it does what I want it to.

I've only ever done this with a 1 dim array, you can mod this if you like:
Func _ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
    If $Ascending <> 0 And $Ascending <> 1 Then Return SetError(1)
    For $i = $Start To UBound($nArray) - 2
        Local $SE = $i
        If $Ascending = 0 Then
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) < Number($nArray[$x]) Then $SE = $x
            Next
        Else
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) > Number($nArray[$x]) Then $SE = $x
            Next
        EndIf
        Local $HLD = $nArray[$i]
        $nArray[$i] = $nArray[$SE]
        $nArray[$SE] = $HLD
    Next
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Im sure _Arraysort can do it in one line though.

I have done it before... But I forget how to set it up.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Im sure _Arraysort can do it in one line though.

I have done it before... But I forget how to set it up.

Unless they fixed it, I don't think it worked with numbers past 9 ...ie... 10 11

If you had

1

2

3

4

5

6

7

8

9

10

and used _ArraySort() you got

1

10

2

3

4

5

6

7

8

9

Which is why I made the _ArraySortNum() :)

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Ill possible come back and try to mod your code later on, its almost 6am here with no sleep : /

Thus the looking for easy answers.

Share this post


Link to post
Share on other sites

Ill possible come back and try to mod your code later on, its almost 6am here with no sleep : /

Thus the looking for easy answers.

show some of your code so we can help.

You can do it with the included _ArraySort() but need to remember to store the values as numbers not as string ..


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

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