Чтобы отсортировать массив по высоте в JavaScript, вы можете использовать различные методы. Вот несколько примеров:
-
Использование метода
sort():const heights = [180, 160, 175, 165, 170]; heights.sort((a, b) => a - b); console.log(heights); -
Использование метода
localeCompare():const heights = [180, 160, 175, 165, 170]; heights.sort((a, b) => a.toString().localeCompare(b.toString())); console.log(heights); -
Использование метода
reduce():const heights = [180, 160, 175, 165, 170]; const sortedHeights = heights.reduce((acc, curr) => { const index = acc.findIndex((height) => height > curr); if (index === -1) { acc.push(curr); } else { acc.splice(index, 0, curr); } return acc; }, []); console.log(sortedHeights);
Это всего лишь несколько примеров. Существует множество других способов добиться того же результата.