Help

JS-ARRAY-SORT

The Array.sort() method either takes an optional comparison function as an argument. The comparison function takes two values (left and right), and the sorting function should return whether or not left comes before or after right in your sort. If the comparison function returns < 0, then it means that left comes before right. If the comparison function returns > 0, then it means that right comes before left. If the comparison function returns 0, then it means left and right are the same and it doesn't matter which order they appear in.

If the sort function isn't supplied, a default sort will be done. The following room behavior code provides an example:

// first build up an array of everyone in my group
var myArray = new Array();
actor.visitGroup( myArray );

// set up how we want to display the group members
function displayHit( groupie ) {
  self.msg( actor.shortDesc + "'s group has "
    + groupie + " who has " + groupie.hit + " hitpoints." );
}

// the following sorts the people in your group alphabetically by name
myArray.sort();
myArray.visit( displayHit );

// the following sorts the people in your group according to how many
// hitpoints they have
function sortByHitPoints( left, right ) {
  return left.hit - right.hit;
}
myArray.sort( sortByHitPoints );
myArray.visit( displayHit );