# Sorting Array using MAIN and Secondary index

## Recommended Posts

Hi!

This is my small contribution.

This code will first generate a 2 column array, and random some values. It will then sort the array using the 2nd column, and then sort some more using the 1st column. I have searched a lot and didnt found anything so i build mine!

TO DO: Use UBOUND(ARRAY) to find the array size

```#include <array.au3>
;create Sample Array
Local \$Array
\$Array = "index"
For \$i = 1 To 99
\$Array[\$i] = Int(Random() * 5) * 10
\$Array[\$i] = Int(Random() * 5) * 10
Next
\$Array = 33
\$Array = 33
\$Array = 22
\$Array = 22
_ArrayDisplay(\$Array,"Before")
\$Main_Index = 2
\$Secondary_Index = 1
_ArraySort(\$Array, 0, 0, 0, \$Main_Index)
\$start = 1
Do
\$i = \$start
If \$Array[\$i][\$Main_Index] = \$Array[\$i + 1][\$Main_Index] Then
Do
\$i += 1
Until \$i = 99 Or \$Array[\$i][\$Main_Index] <> \$Array[\$i + 1][\$Main_Index]
_ArraySort(\$Array, 1, \$start, \$i, \$Secondary_Index);small sort secondary index
EndIf
\$start = \$i + 1
Until \$start >= 98 ;Array size -1
_ArrayDisplay(\$Array,"After")```

Hope it helps

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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