# sort a two dim array

## Recommended Posts

Hi!

I have a two dimensional array with the data, values and colors. It looks like this:

\$array[0][0] = "Jon"

\$array[0][1] = "45" ; percentage value

\$array[0][2] = "0xFFFF00"

\$array[1][0] = "gafrost"

\$array[1][1] = "15" ; percentage value

\$array[1][2] = "0x00FF33"

\$array[2][0] = "Mr. X"

\$array[2][1] = "35" ; percentage value

\$array[2][2] = "0xCCCCCC"

This two-dim array is ought to be sorted (looking on the percentage values) now. Afterwards it shall be like this:

\$array[0][0] = "Jon"

\$array[0][1] = "45" ; percentage value

\$array[0][2] = "0xFFFF00"

\$array[1][0] = "Mr. X"

\$array[1][1] = "35" ; percentage value

\$array[1][2] = "0xCCCCCC"

\$array[2][0] = "gafrost"

\$array[2][1] = "15" ; percentage value

\$array[2][2] = "0x00FF33"

I hope you can help me,

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

##### Share on other sites

heres an idea ( if i got your question correct)

```#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>

Dim \$String_[4], \$Test_[4]

\$String_[1] = "2"
\$String_[2] = "2"
\$String_[3] = "Andy"

for \$x = 1 to 3
\$Test_[\$x] = StringUpper ( \$String_[\$x] )

Next

If \$test_[1] >= \$test_[2] And \$test_[1] >= \$test_[3] And  \$test_[2] >= \$test_[3] Then
\$Final_1 = _StringProper ( \$String_[1] )
\$Final_2 = _StringProper ( \$String_[2] )
\$Final_3 = _StringProper ( \$String_[3] )
EndIf

If \$test_[1] >= \$test_[2] And \$test_[1] >= \$test_[3] And  \$test_[3] >= \$test_[2] Then
\$Final_1 = _StringProper ( \$String_[1] )
\$Final_2 = _StringProper ( \$String_[3] )
\$Final_3 = _StringProper ( \$String_[2] )
EndIf

If \$test_[2] >= \$test_[1] And \$test_[2] >= \$test_[3] And  \$test_[1] >= \$test_[3] Then
\$Final_1 = _StringProper ( \$String_[2] )
\$Final_2 = _StringProper ( \$String_[1] )
\$Final_3 = _StringProper ( \$String_[3] )
EndIf

If \$test_[2] >= \$test_[1] And \$test_[2] >= \$test_[3] And  \$test_[3] >= \$test_[1] Then
\$Final_1 = _StringProper ( \$String_[2] )
\$Final_2 = _StringProper ( \$String_[3] )
\$Final_3 = _StringProper ( \$String_[1] )
EndIf

If \$test_[3] >= \$test_[1] And \$test_[3] >= \$test_[2] And  \$test_[1] >= \$test_[2] Then
\$Final_1 = _StringProper ( \$String_[3] )
\$Final_2 = _StringProper ( \$String_[1] )
\$Final_3 = _StringProper ( \$String_[2] )
EndIf

If \$test_[3] >= \$test_[1] And \$test_[3] >= \$test_[2] And  \$test_[2] >= \$test_[1] Then
\$Final_1 = _StringProper ( \$String_[3] )
\$Final_2 = _StringProper ( \$String_[2] )
\$Final_3 = _StringProper ( \$String_[1] )
EndIf

MsgBox(0,"Results", "List Order is   " & \$Final_3 & ",  " & \$Final_2 & ",  " & \$Final_1 & "   ")```

where (somehow)

\$String_[1] = \$array[0][1] = "45" ; percentage value

\$String_[2] = \$array[1][1] = "15" ; percentage value

\$String_[3] = \$array[2][1] = "35" ; percentage value

Hope that can help

8)

##### Share on other sites

Something close to this? --

```\$arrayLength = uBound(\$array)
dim \$newArray[\$arrayLength][3]
do

\$max = -1
for \$j = 1 to \$arrayLength
if (\$array[\$j][1] > \$max) then \$max = \$array[\$j][1]
next

if (\$max != -1) then
for \$j = 1 to \$arrayLength
if (\$array[\$j][1] = \$max) then
\$newArray[\$i][0] = \$array[\$j][0]
\$newArray[\$i][1] = \$array[\$j][1]
\$newArray[\$i][2] = \$array[\$j][2]
\$array[\$i][1] = -1
endIf
next
endIf

until (\$max = -1)```

Oops, Robert beat me to it!

Added an extra check. Also fixed a damn copy-and-paste error.

Edited by LxP
##### Share on other sites

Hi!

@valuater:

If I interprete it right, This only works for the case of three items. I want it to be independent of the array's size.

Moreover I would like the array itself to contain the result afterwards. Perhaps there is a was using _ArraySort, but I did not find it.

Thanks, peethebee

Edited by peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

##### Share on other sites

Well I try.... ( with my hobbiest approach)

LxP has a good developers approach... and it looks great

good luck

8)

##### Share on other sites

Hi again!

@LxP:

Does this algorithm find only the maximum or does it go through all values?

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

##### Share on other sites

LxP

for \$j = 1 to arrayLength

is

for \$j = 1 to \$arrayLength

???

8)

##### Share on other sites

Does this algorithm find only the maximum or does it go through all values?

<{POST_SNAPBACK}>

For each iteration of the main loop it will find the maximum percentage to still exist, transfer that to a new array and remove it from the old array. (In short, it goes through all values.)

for \$j = 1 to arrayLength

is

for \$j = 1 to \$arrayLength

???

Ack! Thanks for pointing that out. Better fix it.
##### Share on other sites

Ah, I got it.

I missed this line.

`\$array[\$i][1] = -1`

Thank you very much, LxP, how have you been able to code this in such a short time??

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

##### Share on other sites

• Developers

Why not use the buildin _ArraySort() UDF ??

```#include<array.au3>
Dim \$array[3][3]
\$array[0][0] = "Jon"
\$array[0][1] = "45"; percentage value
\$array[0][2] = "0xFFFF00"

\$array[1][0] = "gafrost"
\$array[1][1] = "15"; percentage value
\$array[1][2] = "0x00FF33"

\$array[2][0] = "Mr. X"
\$array[2][1] = "35"; percentage value
\$array[2][2] = "0xCCCCCC"
;
_ArraySort(\$Array,1,0,0,2,1)

For \$x = 0 to 2
ConsoleWrite('\$array = ' & \$array[\$x][0] & @lf)
Next```

SciTE4AutoIt3 Full installer Download page Â  -Â Beta filesÂ  Â  Â  Â  Â  Â
Â
Live for the present,
Dream of the future,
Learn from the past.
Â

##### Share on other sites

Thank you very much, LxP, how have you been able to code this in such a short time??

<{POST_SNAPBACK}>

Must have had to do something similar for the 5,000-line Java assignment I'm working on... Edited by LxP
##### Share on other sites

Hi!

@JdeB

I was trying to, but I didn't get it to run :-(

Thank you.

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

##### Share on other sites

Hi!

LxP's solution works, but the code needed some minor changes. Here's what worked at me:

```Func _ArraySort2Dim(\$_stat_array, \$2dim_pos, \$reverse = 0)
\$_stat_arraylength = UBound(\$_stat_array) - 1
Dim \$newarray[\$_stat_arraylength + 1][3]
\$counter = 0

Do
\$max = -1000000
For \$j = 0 To \$_stat_arraylength
if (\$_stat_array[\$j][\$2dim_pos] > \$max) Then \$max = \$_stat_array[\$j][\$2dim_pos]
Next
if (\$max <> - 1000000) Then
For \$j = 0 To \$_stat_arraylength
If (\$_stat_array[\$j][\$2dim_pos] = \$max) Then
If \$reverse = 0 Then
\$newarray[\$counter][0] = \$_stat_array[\$j][0]
\$newarray[\$counter][1] = \$_stat_array[\$j][1]
\$newarray[\$counter][2] = \$_stat_array[\$j][2]
\$_stat_array[\$j][\$2dim_pos] = -1000000
Else
\$newarray[\$_stat_arraylength - \$counter][0] = \$_stat_array[\$j][0]
\$newarray[\$_stat_arraylength - \$counter][1] = \$_stat_array[\$j][1]
\$newarray[\$_stat_arraylength - \$counter][2] = \$_stat_array[\$j][2]
\$_stat_array[\$j][\$2dim_pos] = -1000000
EndIf
\$counter = \$counter + 1
EndIf
Next
EndIf
until (\$max = -1000000)

Return \$newarray
EndFunc ;==>_ArraySort2Dim```
Edited by peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...