# 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 = "Jon"

\$array = "45" ; percentage value

\$array = "0xFFFF00"

\$array = "gafrost"

\$array = "15" ; percentage value

\$array = "0x00FF33"

\$array = "Mr. X"

\$array = "35" ; percentage value

\$array = "0xCCCCCC"

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

\$array = "Jon"

\$array = "45" ; percentage value

\$array = "0xFFFF00"

\$array = "Mr. X"

\$array = "35" ; percentage value

\$array = "0xCCCCCC"

\$array = "gafrost"

\$array = "15" ; percentage value

\$array = "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_, \$Test_

\$String_ = "2"
\$String_ = "2"
\$String_ = "Andy"

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

Next

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

If \$test_ >= \$test_ And \$test_ >= \$test_ And  \$test_ >= \$test_ Then
\$Final_1 = _StringProper ( \$String_ )
\$Final_2 = _StringProper ( \$String_ )
\$Final_3 = _StringProper ( \$String_ )
EndIf

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

where (somehow)

\$String_ = \$array = "45" ; percentage value

\$String_ = \$array = "15" ; percentage value

\$String_ = \$array = "35" ; percentage value

Hope that can help

8) ##### Share on other sites

Something close to this? --

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

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

if (\$max != -1) then
for \$j = 1 to \$arrayLength
if (\$array[\$j] = \$max) then
\$newArray[\$i] = \$array[\$j]
\$newArray[\$i] = \$array[\$j]
\$newArray[\$i] = \$array[\$j]
\$array[\$i] = -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`

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
\$array = "Jon"
\$array = "45"; percentage value
\$array = "0xFFFF00"

\$array = "gafrost"
\$array = "15"; percentage value
\$array = "0x00FF33"

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

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

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]
\$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] = \$_stat_array[\$j]
\$newarray[\$counter] = \$_stat_array[\$j]
\$newarray[\$counter] = \$_stat_array[\$j]
\$_stat_array[\$j][\$2dim_pos] = -1000000
Else
\$newarray[\$_stat_arraylength - \$counter] = \$_stat_array[\$j]
\$newarray[\$_stat_arraylength - \$counter] = \$_stat_array[\$j]
\$newarray[\$_stat_arraylength - \$counter] = \$_stat_array[\$j]
\$_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...