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 код:
returnSortedArrayarr[ ], lenretArr[ ] )  {
    new 
lowIdx 0tmpVar;
    for( new 
ilen++ ) {
        for( new 
len 1> -1--) {
            if( 
arr] < arrlowIdx ] )  {
                
tmpVar arrlowIdx ];
                
arrlowIdx ] = arr];
                
arr] = tmpVar;
            } 
        }
        
lowIdx++;
    }
    for( new 
ilen++ ) {
        
retArr] = arr];
    }    
    
retArr] = arrlen ];
    return 
true;

It might be not so optimized, but well, it works.

Example usage:

PHP код:
    new 
        
arr13 ] = { 8767865348651105333424454131659496, -11178548543210517, -13375478598947897495489 },
        
retArr13 ]
    ;
    
returnSortedArrayarrsizeof arrretArr);
    for( new 
i13++ )
    {
        
printf("%i\n"retArr]);
    }