It looks like you're new here. If you want to get involved, click one of these buttons!
<<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>>
<<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>>
Comments
I'm not sure about the other sort methods, but if you want to sort everything from A-Z, all you need is
My mistake! I forgot you were sorting objects, not just array variables.
JavaScript inside the <<script>> tags will not pull variables from outside the <<script>> tags. Your solution needs to either happen entirely inside of JavaScript (using functions to pull info from the HTML in order to determine the sorting method and to grab the array), or it needs to be done using just Twine and SugarCube.
It might help if you post the rest of the relevant code so we can see how you're defining everything and filling up the array.
I was not able to figure out sorting by name, but I did get it for the age sorting.
Note: When you use the .sort() method, it automatically assigns the new array. You only need <<set $array.sort()>> to get the new array, rather than <<set $array to $array.sort()>>.
Here's what I have:
That works for both age sorting, but not for names. I'm not sure how to get the name sorting working.
You didn't show all of the code involved, so it's difficult to say what is wrong.
As noted in the <<script>> macro's documentation, it evaluates pure JavaScript code, not TwineScript. To access story variables within a <<script>> invocation, you'd need to use the State.variables property or the variables() function. For example:
You're four releases behind—SugarCube v2.11.0 is the current release.
That's not quite correct—so, no and yes. No—the <Array>.sort() method sorts the existing/original array in-place and returns a reference to it, it does not generate a new array. Yes—since it sorts in-place, there's no need for an assignment, though there's no harm in doing so.
Anyway. The following works for me: All sorts were tested and verified as working. I used a temporary variable to hold the selected sorting function, rather than repeating the call to <Array>.sort() multiple times, but that doesn't change how anything works.
That's not fundamentally different from your original example, so for now I'm going to assume that your issue lies within the structure of $Array itself.