Order from highest to lowest - 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: Order from highest to lowest (
/showthread.php?tid=621019)
Order from highest to lowest -
GranaT3 - 05.11.2016
How I can order from highest to lowest values?
Re: Order from highest to lowest -
SyS - 06.11.2016
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 (i = 0; i < sizeof(number);i++)
{
for (j = i + 1; j < sizeof(number); j++)
{
if (number[i] < number[j])//to sort in reverse (ascending change < to >)
{
a = 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
Re: Order from highest to lowest -
SickAttack - 06.11.2016
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;
}
Respuesta: Order from highest to lowest -
GranaT3 - 06.11.2016
Thank you. I had seen that algorithm before but did not think.