Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sorting objects inside an array by a specific property

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

  • On an only slightly relevant note, I've run into an issue with another array, that contains only strings. I'm trying to call one specific string from the array, based on a variable.

    $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.
  • The Naked Variable documentation states you need to use the <<print>> macro if you want to print the result of an expression, and $Variable + 1 is an expression.

    try:
    <<print $Array[$Variable + 1]>>
    
  • edited September 2016
    PostingAlt wrote: »
    How would I go about sorting the objects in the array by a given property?
    You'll have to write a comparison function. For example:
    /* Case insensitive sort (A→Z). */
    $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;
    });
    
    Caveat: Does not handle extended characters well.

    And some number sorting comparison functions.
    /* Numeric sort (ascending). */
    $array.sort(function (a, b) { return a.value - b.value; });
    
    /* Numeric sort (descending). */
    $array.sort(function (a, b) { return b.value - a.value; });
    

    PostingAlt wrote: »
    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?
    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.

    PostingAlt wrote: »
    On an only slightly relevant note, I've run into an issue with another array, that contains only strings. I'm trying to call one specific string from the array, based on a variable.
    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:
    <<=$Array[$Variable + 1]>>
    
  • Thanks for both of your replies.

    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.
Sign In or Register to comment.