SA-MP Forums Archive
How to sort an array by number - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to sort an array by number (/showthread.php?tid=618126)



How to sort an array by number - NeXoR - 01.10.2016

Heya, let's say I have a ten-cells dialog with numbers in it, how can I re-sort it to make the lowest number on array[0] and the highest on [9] ?


Re: How to sort an array by number - Kaliber - 01.10.2016

******...was a nice invention..use it: http://forum.sa-mp.com/showpost.php?...postcount=1737


Re: How to sort an array by number - Jayse - 01.10.2016

I made a function earlier to sort arrays, because I really wanted to do my own instead of using RyDeR's one.

PHP код:
returnSortedArray( arr[ ], len, retArr[ ] )  {
    new 
lowIdx = 0, tmpVar;
    for( new 
i; i < len; i ++ ) {
        for( new 
j = len - 1; j > -1; j --) {
            if( 
arr[ j ] < arr[ lowIdx ] )  {
                
tmpVar = arr[ lowIdx ];
                
arr[ lowIdx ] = arr[ j ];
                
arr[ j ] = tmpVar;
            } 
        }
        
lowIdx++;
    }
    for( new 
i; i < len; i ++ ) {
        
retArr[ i + 1 ] = arr[ i ];
    }    
    
retArr[ 0 ] = arr[ len - 1 ];
    return 
true;
} 
It might be not so optimized, but well, it works.

Example usage:

PHP код:
    new 
        
arr[ 13 ] = { 876786, 53486, 51105, 333424, 454131, 659496, -1117, 854854, 3210517, -1337, 54785, 989478974, 95489 },
        
retArr[ 13 ]
    ;
    
returnSortedArray( arr, sizeof arr, retArr);
    for( new 
i; i < 13; i ++ )
    {
        
printf("%i\n", retArr[ i ]);
    }