add arr.sort
This commit is contained in:
		| @@ -34,4 +34,18 @@ console.log(someUsers); | ||||
|  | ||||
| // arr.map(fn) | ||||
| let lengths = array.map(item=>item.length); | ||||
| alert(lengths); | ||||
| console.log(lengths); | ||||
|  | ||||
| // arr.sort(fn) | ||||
| function compare(a, b){ | ||||
|     if (a > b) return 1; | ||||
|     if (a == b) return 0; | ||||
|     if (a < b) return -1; | ||||
| } | ||||
|  | ||||
| arr = [15, 2 ,1]; | ||||
| console.log(`arr.sort(): ${arr.sort(compare)}`); | ||||
| console.log(`arr.sort(): ${arr.sort((a,b) => a - b)}`); | ||||
|  | ||||
| let countries = ['Österreich', 'Andorra', 'Vietnam']; | ||||
| console.log(countries.sort((a,b) => a.localeCompare(b))) | ||||
		Reference in New Issue
	
	Block a user