Order from highest to lowest
#1

How I can order from highest to lowest values?
Reply
#2

If you meant values in an array you can easily sort them in descending order.There are lot of sorting methods available.I will show you simple sorting technique (Linear sort)
PHP код:
new i,j,a;    
for (
0sizeof(number);i++)
    {
        for (
1sizeof(number); j++)
        {
            if (
number[i] < number[j])//to sort in reverse (ascending change < to >) 
            
{
                
number[i];
                
number[i] = number[j];
                
number[j] = a;
            }
        }
    } 
There are also more optimized algorithms for sorting like insertion,bubble,shell etc.You can ****** about it
Reply
#3

Fastest with large arrays: http://forum.sa-mp.com/showpost.php?...postcount=1737

Other:
pawn Код:
// ** INCLUDES

#include <a_samp>

// ** MAIN

main()
{
    print("Loaded \"sort_values_in_array.amx\"."); // 430

    /*
    new tick = GetTickCount();
    for(new i = 0; i < 50000; i ++)
    {
        new array[] = {30, 100, 20, 40, 5, 23, 56, 21, 2000, 40, 60, 20, 70, 80, 290, 5000};
        SortValuesInArray(array);
    }

    printf("%d", GetTickCount() - tick);
    */


    ///*
    new array[] = {30, 100, 20, 40, 5, 23, 56, 21, 2000, 40, 60, 20, 70, 80, 290, 5000};
    SortValuesInArray(array);

    for(new i = 0; i < sizeof(array); i ++)
    {
        printf("%d", array[i]);
    }
    //*/
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

stock SortValuesInArray(array[], size_of = sizeof(array))
{
    for(new i = (size_of - 1), j, temp; i != 0; i --)
    {
        for(j = 0; j < i; j ++)
        {
            if(array[i] < array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    return 1;
}
Reply
#4

Thank you. I had seen that algorithm before but did not think.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)