How to sort an array by number
#1

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] ?
Reply
#2

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

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 ]);
    } 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)