As the title says, I have an array that contains multiple objects. The objects are all similar, with the same properties but with different values for each - for example, they all have a "Name", but the string of that property is different for each object. How would I go about sorting the objects in the array by a given property?
I'm guessing it will take a more specific form of the array.sort() method, which I believe would work in that form if the array contained variables rather than objects. If relevant, I'd need one method for sorting text strings alphabetically, and another for sorting numbers in ascending/descending order.
As a side note, if two different objects were to have the same value for the property they are being sorted by - if the "Name" for each was "Kevin", for example - how would this method sort those two objects?
I am using SugarCube 2.7.2 on Twine 2.0.11
Comments
$Array[1] works fine. $Array[$Variable] works fine.
$Array[$Variable + 1] does not work, neither does $Array[`$Variable + 1`] or $Array["$Variable + 1"]. In each of these cases, the entire array is printed, followed by the sum in square brackets (eg [1 + 1], [`1 + 1`] as appropriate).
I'm sure the solution is simple, but I am having a blonde moment here.
try:
And some number sorting comparison functions.
Depends on the browser. The standard says that a and b should be unchanged with respect to each other, while being sorted with respect to the other elements. That said, the standard does not guarantee that behavior, as browser implementations have varied in this respect.
Are you attempting to print the value without using one of the print macros?
Assuming you are, the naked variable markup is intended to easily allow the printing of variables—e.g. $foo. Once you start engaging in compound expressions, method calls, and the like, the markup calls it quits. As noted in its documentation, in such cases you must use the one of the print macros: <<print>>, <<=>>, <<->>.
For example:
The issue was indeed the lack of a print macro. The naked variable markup has led to be becoming quite lazy about things like this.
I've not been able to fully test those functions due to an unrelated error, but they look good, thanks.