Jump to content

Sorting a two dimensional array


 Share

Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

  • Developers

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 ..

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...