# Sorting Numbers

## Recommended Posts

So yeah... i'm trying to make a script that gives me medians... but I can't sort a list of numbers because it will put multiple-digit numbers in the wrong place.

For example, I put in 1,2,3,10,22,31 in _ArraySort and it gives me

1,10,2,22,3,31

Any way I can put these numbers in order of least to greatest?

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

##### Share on other sites

So yeah... i'm trying to make a script that gives me medians... but I can't sort a list of numbers because it will put multiple-digit numbers in the wrong place.

For example, I put in 1,2,3,10,22,31 in _ArraySort and it gives me

1,10,2,22,3,31

Any way I can put these numbers in order of least to greatest?

```Func _ArraySortNum(ByRef \$n_array, \$i_descending = 0, \$i_start = 1)
Local \$i_ub = UBound(\$n_array)
For \$i_count = \$i_start To \$i_ub - 2
Local \$i_se = \$i_count
If \$i_descending Then
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) < Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
Else
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) > Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
EndIf
Local \$i_hld = \$n_array[\$i_count]
\$n_array[\$i_count] = \$n_array[\$i_se]
\$n_array[\$i_se] = \$i_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.

##### Share on other sites

```Func _ArraySortNum(ByRef \$n_array, \$i_descending = 0, \$i_start = 1)
Local \$i_ub = UBound(\$n_array)
For \$i_count = \$i_start To \$i_ub - 2
Local \$i_se = \$i_count
If \$i_descending Then
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) < Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
Else
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) > Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
EndIf
Local \$i_hld = \$n_array[\$i_count]
\$n_array[\$i_count] = \$n_array[\$i_se]
\$n_array[\$i_se] = \$i_hld
Next
EndFunc```

Hi

How do we use this function ?

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

##### Share on other sites

Like This...

```#include <array.au3>

\$Array = StringSplit("2,5,3,4,6,1,8,9,7", ",")

_ArraySortNum(\$Array)

_ArrayDisplay(\$Array, "Sorted Array")

Func _ArraySortNum(ByRef \$n_array, \$i_descending = 0, \$i_start = 1)
Local \$i_ub = UBound(\$n_array)
For \$i_count = \$i_start To \$i_ub - 2
Local \$i_se = \$i_count
If \$i_descending Then
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) < Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
Else
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) > Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
EndIf
Local \$i_hld = \$n_array[\$i_count]
\$n_array[\$i_count] = \$n_array[\$i_se]
\$n_array[\$i_se] = \$i_hld
Next
EndFunc   ;==>_ArraySortNum```

8)

##### Share on other sites

Like This...

```#include <array.au3>

\$Array = StringSplit("2,5,3,4,6,1,8,9,7", ",")

_ArraySortNum(\$Array)

_ArrayDisplay(\$Array, "Sorted Array")

Func _ArraySortNum(ByRef \$n_array, \$i_descending = 0, \$i_start = 1)
Local \$i_ub = UBound(\$n_array)
For \$i_count = \$i_start To \$i_ub - 2
Local \$i_se = \$i_count
If \$i_descending Then
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) < Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
Else
For \$x_count = \$i_count To \$i_ub - 1
If Number(\$n_array[\$i_se]) > Number(\$n_array[\$x_count]) Then \$i_se = \$x_count
Next
EndIf
Local \$i_hld = \$n_array[\$i_count]
\$n_array[\$i_count] = \$n_array[\$i_se]
\$n_array[\$i_se] = \$i_hld
Next
EndFunc   ;==>_ArraySortNum```

8)

Very thanks

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

##### Share on other sites

Hi.

Quite a time ago I had to search for all elements in one large for all the occurences in another, really *HUGE* array.

To Speed up the whole process, I sorted both Arrays, preserving positions in the 2nd arrays's element list (2D). By this I could skip the searching in the 2nd Array after looking up some few elements and increasing the "start value Position" in the 2nd Array to the next plausible one

Duplicates in both Arrays are possible.

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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