JSFiddle - React, Tailwind, and code Playground
by Cody Lindley
HTML
<script src="https://dl.dropboxusercontent.com/u/2026291/firebug-lite-debug.js"></script>
JavaScript
var sortByAge = function(element, nextElement) {
if (element.age > nextElement.age){return 1;}
if (element.age < nextElement.age){return -1;}
return 0; // element must be equal to nextElement
};
array0 = [{name:'joe',age:93},{name:'jes',age:43},{name:'jan',age:3}];
console.log(array0.sort(sortByAge));
/*logs [Object {name="jan", age=3}, Object {name="jes", age=43}, Object {name="joe", age=93}]*/
// Note:
// sorted lexicographically (in dictionary order) according to the string conversion of each element
array1 = [2,4,6,3];
array2 = ['fish','cat','dog'];
console.log(array1.sort()); //[2, 3, 4, 6]
console.log(array2.sort()); //["cat", "dog", "fish"]