15.10.2010, 20:36
Info (sortWords)
Sorts words in an array according the option. (0: Z to A || 1: A to Z)
Code
Examples
Example, sorting some names from A to Z:
Will sort the array like:
Otherwise if we use the same but then as setting 0 for A_to_Z. Then it will sort from Z to A, so:
NOTE: Please note that you can increase the value of the array size if need. But keep in mind, it's recommended to keep it as low as possible.
Sorts words in an array according the option. (0: Z to A || 1: A to Z)
Code
pawn Код:
stock sortWords(words[][], A_to_Z, size = sizeof(words))
{
for(new x; x != size - 1; ++x)
{
for(new y = x; y != size; ++y)
{
if((!A_to_Z) ? (strcmp(words[x], words[y]) < 0) : (strcmp(words[x], words[y]) > 0))
{
new
string[16]
;
format(string, sizeof(string), words[x]);
format(words[x], sizeof(string), words[y]);
format(words[y], sizeof(string), string);
}
}
}
return 1;
}
Example, sorting some names from A to Z:
pawn Код:
new
names[][16] =
{
"Benjamin",
"Andreas",
"Ryan",
"Jason",
"Michael",
"Brad",
"Dean",
"Thomas",
"Alex",
"Ashley",
"Lovely",
"Carl",
"Johnattan",
"Macy",
"Vanessa",
"Eddy",
"Curtis",
"Alan",
"Lacy",
"Faith",
"Dianna",
"Esmee",
"Glenn",
"Jordy",
"Yasmin",
"Tracy",
"Zena",
"Wendy"
}
;
sortWords(names, 1);
for(new i; i != sizeof(names); ++i)
{
printf("%s", names[i]);
}
pawn Код:
Alan
Alex
Andreas
Ashley
Benjamin
Brad
Carl
Curtis
Dean
Dianna
Eddy
Esmee
Faith
Glenn
Jason
Johnattan
Jordy
Lacy
Lovely
Macy
Michael
Ryan
Thomas
Tracy
Vanessa
Wendy
Yasmin
Zena
pawn Код:
Zena
Yasmin
Wendy
Vanessa
Tracy
Thomas
Ryan
Michael
Macy
Lovely
Lacy
Jordy
Johnattan
Jason
Glenn
Faith
Esmee
Eddy
Dianna
Dean
Curtis
Carl
Brad
Benjamin
Ashley
Andreas
Alex
Alan