I just discovered sortbyvalue() today, but it seems to be extremely inconsistent.
Just playing around with it, I've come across cases where it doesn't sort correctly.
For example, this NPC:
PHP Code:
function onCreated() {
temp.ui = {
{"item2", 1},
{"item1", 1},
};
//myfunc(temp.ui);
for(temp.i = 0; temp.i < temp.ui.size(); temp.i++) {
temp.ui[temp.i].sorted_by = temp.ui[temp.i][0];
}
temp.ui.sortbyvalue("sorted_by", "string", true);
echo(temp.ui);
}
function myfunc(arr) {
for(temp.i = 0; temp.i < temp.ui.size(); temp.i++) {
temp.arr[temp.i].sorted_by = temp.arr[temp.i][0];
}
temp.arr.sortbyvalue("sorted_by", "string", true);
echo(temp.arr);
}
If only one sortbyvalue() process is used, as above, it works perfectly. In other words, if you decide to use myfunc() and comment out the sorting in onCreated(), it works fine. The same for if you don't use myfunc().
However, if you use
both (i.e., removing the commented myfunc() and running script), the array is no longer sorted.
Also, the following script outputs an incorrectly sorted array. It works if you remove the 'before' echo, however.
PHP Code:
function onCreated() {
temp.ui = {
{"item2", 1},
{"item1", 1},
};
echo("before: " @ temp.ui);
for(temp.i = 0; temp.i < temp.ui.size(); temp.i++) {
temp.ui[temp.i].sorted_by = temp.ui[temp.i][0];
}
temp.ui.sortbyvalue("sorted_by", "string", true);
echo("after: " @ temp.ui);
}