Sign in to follow this  
Followers 0
KiwiCoder

Sorting array's issue

6 posts in this topic

Hello guys,

this is my first post so ill start by saying that Autoit is a great scripting language.

now, I'm having some hard time sorting an array based on another array's sort and need your help.

ex;

array1 : [28,00,12,41,21,01,10,79,70,78,96]

array2 : [27,1577,52,26,56,34,21,7,4,7,3] (the number of occurrences of each index of array1 in a file)

i want to sort array1 in descending order based on the number of occurrences (array2)

to do that, first I will sort array2 in descending order ex: [1577,56,52,34,27,26,21,7,7,4,3]…easy done using _arraysort($x,1)

but what's hard is how to make array1 follow the same sort order of array2? knowing that I cannot use _arraysearch to map the changes to array2 after sort because of repeat values, ex: 7 is repeated twice in array2.

any idea's?

Share this post


Link to post
Share on other sites



I dont be sure if i get what you want..

1 array have some values that maybe repeats along the other indexes of the array

example:

$aArray1[20] = [2,5,35,45,2,9,2,84,5,2,52,45,45,9,9,2,48,2,84,2]

And you have a second array with the amount of ocurrences.

example:

$aArray1[5] = [6,2,1,3,2]

Did you want to sort the $aArray1 by the amount of ocurrences but using the $aArray2 as guide?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I dont be sure if i get what you want..

1 array have some values that maybe repeats along the other indexes of the array

example:

$aArray1[20] = [2,5,35,45,2,9,2,84,5,2,52,45,45,9,9,2,48,2,84,2]

And you have a second array with the amount of ocurrences.

example:

$aArray1[5] = [6,2,1,3,2]

Did you want to sort the $aArray1 by the amount of ocurrences but using the $aArray2 as guide?

not exactly, but something like that

array1 has 11 numbers, these numbers are randomly extracted from a text file.

array2 is the result of checking how many times each number in array 1 occur in that text file and contains 11 numbers respectively.

now I want to sort array1 based on the sort of array2 in descending order (ex: the highest number in array2 will be sorted and shifted to index 1 in array2 and I want the same to happen to the corresponding values in array 1)

simply i want to sort an array based on the way another array was sorted. i cant use _arraysort() on array1 because it depends on array2.

ex:

before:

array1 : [28,00,12,41,21,01,10,79,70,78,96]

array2 : [27,1577,52,26,56,34,21,7,4,7,3]

after:

array1: [00,21,12,01,28,41,10,79,78,70,96] this is what i want, i want this array to be sorted the same way as array 2 was sorted as if they are linked together.

array2: [1577,56,52,34,27,26,21,7,7,4,3] after sorted with _arraysort()

hope its more clear now

Edited by KiwiCoder

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I'm thinking the easiest way, if you have the option, would be to build a 2-dimension array in the first place with columns for both $value and $count. Then when you sort the array on column 2 ($count) the data in column 1 ($value) will sort along with column 2, keeping the "pairs" of data together.

If you're stuck with two separate arrays, then you'd need to employ a simple manual bubble sort on $array2 (rather than using _ArraySort), but duplicate the 3 lines of code that swap the values (via a temporary variable) so that any swap made to $array2 would also occur in $array1. That would keep them in sync.

Edit: Alternately, you could simulate the first method, after-the-fact. You can redim one array to two dimensions (without loosing the data) then do a straight copy of the data from the other array into the newly created column, prior to the sort. Then a manual sort of the 2-d array would keep the data 'tied' together.

Edit2: I lie! You'd need to create a third "work" array, I was convinced you could expand an array dimesion and retain the data in column1 (seems like one ought to be able to... maybe ReDim could use some work?)

#include <Array.au3> ; for testing

Global $array1[11] =  [28,00,12,41,21,01,10,79,70,78,96]
Global $array2[11] =  [27,1577,52,26,56,34,21,7,4,7,3]

$arraysize = UBound($array1)
Global $array3[$arraysize][2]


For $x = 0 To $arraysize - 1
    $array3[$x][0] = $array1[$x]
    $array3[$x][1] = $array2[$x]
Next
_ArrayDisplay($array3) ; for testing

_ArraySort($array3, 1, 0, 0, 1)
_ArrayDisplay($array3) ; for testing
Edited by Spiff59

Share this post


Link to post
Share on other sites

Put them in a 2D array, then use the sub item of which array you want to sort by.

Example:

#include <Array.au3>

Global $ga_Arr1[11] = [28,00,12,41,21,01,10,79,70,78,96]
Global $ga_Arr2[11] = [27,1577,52,26,56,34,21,7,4,7,3]

Global $gs_ConsoleWriteArr1, $gs_ConsoleWriteArr2

Global $ga_Arr2D = _myCombine1DArraysAndSort($ga_Arr1, $ga_Arr2, 1, 0, 0, 1)
If @error Then
    MsgBox(16 + 262144, "Error", "Arrays are not the same number of subscripts!")
    Exit
Else
    _ArrayDisplay($ga_Arr1, "Array #1")
    _ArrayDisplay($ga_Arr2, "Array #2")
    $gs_ConsoleWriteArr1 = "array1: ["
    $gs_ConsoleWriteArr2 = "array2: ["
    For $i = 0 To UBound($ga_Arr1) - 1
        $gs_ConsoleWriteArr1 &= $ga_Arr1[$i] & ","
        $gs_ConsoleWriteArr2 &= $ga_Arr2[$i] & ","
    Next
    ConsoleWrite(StringTrimRight($gs_ConsoleWriteArr1, 1) & "]" & @CRLF & _
        StringTrimRight($gs_ConsoleWriteArr2, 1) & "]" & @CRLF)
EndIf

_ArrayDisplay($ga_Arr2D, "2 dimensional array")

Func _myCombine1DArraysAndSort(ByRef $a_arr1, ByRef $a_arr2, $i_descending = 0, $i_start = 0, $i_end = 0, $i_subitem = 0)

    Local $i_ub1 = UBound($a_arr1)
    Local $i_ub2 = UBound($a_arr2)

    If $i_ub1 = 0 Or $i_ub1 <> $i_ub2 Then
        ; not the same amount of elements
        Return SetError(1, 0, 0)
    EndIf

    Local $a_temparr[$i_ub1][2]
    For $iarg = 0 To $i_ub1 - 1
        $a_temparr[$iarg][0] = $a_arr1[$iarg]
        $a_temparr[$iarg][1] = $a_arr2[$iarg]
    Next

    ; sort 2D array
    _ArraySort($a_temparr, $i_descending, $i_start, $i_end, $i_subitem)

    ; convert byref arrays to new order
    For $iarg = 0 To $i_ub1 - 1
        $a_arr1[$iarg] = $a_temparr[$iarg][0]
        $a_arr2[$iarg] = $a_temparr[$iarg][1]
    Next

    ; return 2D array for giggles
    Return $a_temparr
EndFunc

You can add your formatting ( the 2 digit formatting you show ) after the fact.


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 this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks guys for your elegant solutions, both bubble sort and 2D arrays ideas work like a charm :)

especially SmOke_N, thanks for your time and effort on this simple issue.

very helpful community, I can move on now.

Edited by KiwiCoder

Share this post


Link to post
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
Sign in to follow this  
Followers 0