| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title></title> |
| •5 |
<script type="text/javascript" src="/usr/share/nodejs/thenby/thenBy.min.js"></script> |
| 6 |
<script> |
| •7 |
firstBy(); |
| 8 |
var data = [ |
| 9 |
{ id: 7, name: "Amsterdam", population: 750000, country: "Netherlands" }, |
| 10 |
{ id: 12, name: "The Hague", population: 450000, country: "Netherlands" }, |
| 11 |
{ id: 43, name: "Rotterdam", population: 600000, country: "Netherlands" }, |
| 12 |
{ id: 5, name: "Berlin", population: 3000000, country: "Germany" }, |
| •13 |
{ id: 42, name: "Dusseldorf", population: 550000, country: "Germany" }, |
| 14 |
{ id: 44, name: "Stuttgard", population: 600000, country: "Germany" }, |
| 15 |
]; |
| 16 |
function output() { |
| 17 |
for (var i = 0; i < data.length; i++) { |
| 18 |
document.write(data[i].name + "<br/>"); |
| 19 |
} |
| 20 |
document.write("<br/>"); |
| 21 |
} |
| 22 |
output(); |
| 23 |
|
| •24 |
// first by country, then by pop, should give Dusseldorf first, Amsterdam last |
| 25 |
var s = firstBy(function (v1, v2) { return v1.country < v2.country ? -1 : (v1.country > v2.country ? 1 : 0); }) |
| 26 |
.thenBy(function (v1, v2) { return v1.population - v2.population; }); |
| 27 |
data.sort(s); |
| 28 |
output(); |
| 29 |
// first by length of name, should give Stuttgard just after Rotterdam (same length, same pop) |
| 30 |
s = firstBy(function (v1, v2) { return v1.name.length - v2.name.length; }) |
| 31 |
.thenBy(function (v1, v2) { return v1.population - v2.population; }) |
| 32 |
.thenBy(function (v1, v2) { return v1.id - v2.id; }); |
| 33 |
data.sort(s); |
| 34 |
output(); |
| 35 |
|
| 36 |
// Now we do the same, but using some handy shortcuts |
| •37 |
// first by country, then by pop, should give Dusseldorf first, Amsterdam last |
| 38 |
var s = firstBy("country").thenBy("population"); |
| 39 |
data.sort(s); |
| 40 |
output(); |
| 41 |
// first by length of name, should give Stuttgard just after Rotterdam (same length, same pop) |
| 42 |
s = firstBy(function (v) { return v.name.length;}) |
| 43 |
.thenBy("population") |
| 44 |
.thenBy("id"); |
| 45 |
data.sort(s); |
| 46 |
output(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
</script> |
| 51 |
</head> |
| 52 |
<body> |
| 53 |
</body> |
| 54 |
</html> |