본문 바로가기

Development

[Java Script] Array의 Sort Method

Definition and Usage

정의와 용법

The sort() method is used to sort the elements of an array.

sort() method는 배열의 요소들을 sort하는데 사용되어진다.

Syntax

문법
arrayObject.sort(sortby)

Parameter Description
sortby Optional. Specifies the sort order. Must be a function
선택. 정렬순서의 특별한값. 반드시 함수여야한다.


Tips and Notes

Note: The sort() method will sort the elements alphabetically by default. However, this means that numbers will not be sorted correctly (40 comes before 5). To sort numbers, you must create a function that compare numbers.

sort() method는 기본적으로 요소들의 알파벳순 정렬을 할것이다. 그러나, 이것은 정확히 숫자들이 정렬되어지는것을 의미하는것은 아니다. (40 comes before 5 => 이건 무슨 의미일까?). 숫자들을 정렬하는것은, 반드시 숫자들을 비교하는 함수를 만들어야한다.

Note: After using the sort() method, the array is changed.

sort() method 사용이후에, array는 변경되어진다.


Example 1

In this example we will create an array and sort it alphabetically:
이 예제에서는 array 생성과 그것을 알바벳순으로 sort하는것을 보여준다.




Example 2

In this example we will create an array containing numbers and sort it:
이 예제는 숫자를 포함하고 있는 array를 만들고 그것을 sort하는것을 보여준다.


Note that the numbers above are NOT sorted correctly (by numeric value). To solve this problem, we must add a function that handles this problem:

숫자들은 정확히 위와같이하면 정렬되지 않는다(숫자값). 이문제를 해결하는것은, 이문제를 제어하는 함수를 추가해야만한다.