From a5be34f3ab7acc9d6020a7c16f558198e95f2a27 Mon Sep 17 00:00:00 2001 From: mhrooz Date: Sun, 16 Jun 2024 21:21:58 +0200 Subject: [PATCH] add arr.sort --- 5.4ArrayMethod/t.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/5.4ArrayMethod/t.js b/5.4ArrayMethod/t.js index 26a63cc..a0d43af 100644 --- a/5.4ArrayMethod/t.js +++ b/5.4ArrayMethod/t.js @@ -34,4 +34,18 @@ console.log(someUsers); // arr.map(fn) let lengths = array.map(item=>item.length); -alert(lengths); \ No newline at end of file +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))) \ No newline at end of file