<<if $ArraySort == "Name A-Z">>
<<set $Array to $Array.sort(function(a, b){if(a.Name < b.Name) return -1; if(a.Name > b.Name) return 1; return 0;})>>
<<elseif $ArraySort == "Name Z-A">>
<<set $Array to $Array.sort(function(a, b){if(a.Name > b.Name) return -1; if(a.Name < b.Name) return 1; return 0;})>>
<<elseif $ArraySort == "Age 1-2">>
<<set $Array to $Array.sort(function(a, b){return a.Age-b.Age})>>
<<elseif $ArraySort == "Age 2-1">>
<<set $Array to $Array.sort(function(a, b){return b.Age-a.Age})>>
<</if>>
This is an older project that wasn't fully tested before I mothballed it, that I am now trying to get up and running again.
This is an attempt to sort the objects inside the array, using a method I believe I lifted from another game. All four values for $ArraySort (and thus all four methods of sorting the array) produce the same error message; "Error: <<set>>: bad evaluation: undefined is not a function".
About three lines below that error message is evidence that the array in question does indeed contain objects that have a .Name and a .Age property.
The passage comments contain an alternative method:
<<if $ArraySort == "Name A-Z">>
<<script>>
$Array.sort(function (a, b) {
var
NameA = a.Name.toUpperCase(),
NameB = b.Name.toUpperCase();
if (NameA < NameB) {
return -1;
}
else if (NameA > NameB) {
return 1;
}
return 0;
});
<</script>>
<<elseif $ArraySort == "Name Z-A">>
<<script>>
$Array.sort(function (a, b) {
var
NameA = a.Name.toUpperCase(),
NameB = b.Name.toUpperCase();
if (NameA > NameB) {
return -1;
}
else if (NameA < NameB) {
return 1;
}
return 0;
});
<</script>>
<<elseif $ArraySort == "Age 1-2">>
<<script>>
$Array.sort(function (a, b) {
return a.Age - b.Age;
});
<</script>>
<<elseif $ArraySort == "Age 2-1">>
<<script>>
$Array.sort(function (a, b) {
return b.Age - a.Age;
});
<</script>>
<</if>>
This produces a different error; "<<script>>: bad evaluation: $Array is not defined".
Any idea how to take one or both of these early drafts and turn it into something that works?
In case it isn't obvious, the four 'sorting options' I'm using are alphabetically ascending/descending by the 'Name' property, and numerically ascending/descending by the 'Age' property.
I am using SugarCube 2.7.2 on Twine 2.0.11