let nums = [1, 10, 20, 11, -1, 101, 0];
// 내부적으로 각 요소를 비교할 때, x - y가 양수이면 자리를 바꾸고, 그렇지 않으면 자리 유지
// ex. x = 11, y = 101
// => 11 - 101 = -90 (-90은 음수이므로 자리 유지)
let ascending_order = function (x, y) {
return x - y;
};
console.log(nums.sort(ascending_order));
// output: [ -1, 0, 1, 10, 11, 20, 101 ] (오름차순 정렬 성공!)
// 내부적으로 각 요소를 비교할 때, y - x가 양수이면 자리를 바꾸고, 그렇지 않으면 자리 유지
// ex. x = 11, y = 101
// => 101 - 11 = 90 (90은 양수이므로 자리 바꿈)
let descending_order = function (x, y) {
return y - x;
};
console.log(nums.sort(descending_order));
// output: [ 101, 20, 11, 10, 1, 0, -1 ] (내림차순 정렬 성공!)
- 문제점 2: 대소문자 구분되어 정렬 (sort 메서드로 정렬될 때 ASCII 코드 값 기준으로 정렬되기 때문에 발생하는 문제)
let fruits = ["Orange", "apple", "melon", "orange"];
console.log(fruits.sort());
// output: [ 'Orange', 'apple', 'melon', 'orange' ] (대소문자 구분 안하려면..?)
위 ASCII 코드 표를 보면 모든 대문자가 모든 소문자보다 ASCII 코드 값이 작은 것을 확인할 수 있다.
"Orange"와 "apple"의 경우 각각 가장 앞 글자가 O, a이므로 "apple"이 "Orange"보다 뒤쪽에 정렬되는 것!
- 문제점 2의 해결방법: sort()의 매개변수로 함수를 넣어 해결
let fruits = ["Orange", "apple", "melon", "orange"];
// 내부적으로 각 요소를 비교할 때, 모든 글자를 대문자로 변환한 것끼리 비교
// 반환되는 값이 양수이면 자리 바꿈, 음수이거나 0이면 자리 유지
// ex. x = Orange, y = apple
// => x = ORANGE, y = APPLE
// => ORANGE > APPLE 이므로 1 반환 (1은 양수이므로 자리 바꿈)
let ascending_order = function (x, y) {
x = x.toUpperCase();
y = y.toUpperCase();
if (x > y) return 1;
else if (x < y) return -1;
else return 0;
};
console.log(fruits.sort(ascending_order));
// output: [ 'apple', 'melon', 'Orange', 'orange' ] (대소문자 구분 없이 사전순 정렬 성공!)
// 내부적으로 각 요소를 비교할 때, 모든 글자를 대문자로 변환한 것끼리 비교
// 반환되는 값이 양수이면 자리 바꿈, 음수이거나 0이면 자리 유지
// ex. x = Orange, y = apple
// => x = ORANGE, y = APPLE
// => ORANGE > APPLE 이므로 -1 반환 (-1은 음수이므로 자리 유지)
let descending_order = function (x, y) {
x = x.toUpperCase();
y = y.toUpperCase();
if (x < y) return 1;
else if (x > y) return -1;
else return 0;
};
console.log(fruits.sort(descending_order));
// output: [ 'Orange', 'orange', 'melon', 'apple' ] (대소문자 구분 없이 사전역순 정렬 성공!)
- 문제점 1의 해결방법과 문제점 2의 해결방법 통일
let ascending_order = function (x, y) {
// 비교대상이 문자열일 경우 모든 글자를 대문자로 변환한 것끼리 비교
if (typeof x === "string") x = x.toUpperCase();
if (typeof y === "string") y = y.toUpperCase();
// x가 y보다 크면 1 반환 (양수이므로 자리바꿈), 그렇지 않으면 -1 반환 (음수이므로 자리 유지)
return x > y ? 1 : -1;
};
let descending_order = function (x, y) {
// 비교대상이 문자열일 경우 모든 글자를 대문자로 변환한 것끼리 비교
if (typeof x === "string") x = x.toUpperCase();
if (typeof y === "string") y = y.toUpperCase();
// x가 y보다 작으면 1 반환 (양수이므로 자리바꿈), 그렇지 않으면 -1 반환 (음수이므로 자리 유지)
return x < y ? 1 : -1;
};
let nums = [1, 10, 20, 11, -1, 101, 0];
console.log(nums.sort(ascending_order));
// output: [ -1, 0, 1, 10, 11, 20, 101 ] (숫자 오름차순 정렬 성공!)
console.log(nums.sort(descending_order));
// output: [ 101, 20, 11, 10, 1, 0, -1 ] (숫자 내림차순 정렬 성공!)
let fruits = ["Orange", "apple", "melon", "orange"];
console.log(fruits.sort(ascending_order));
// output: [ 'apple', 'melon', 'orange', 'Orange' ]
// (문자열 대소문자 구분 없이 사전순 정렬 성공!)
console.log(fruits.sort(descending_order));
// output: [ 'Orange', 'orange', 'melon', 'apple' ]
// (문자열 대소문자 구분 없이 사전역순 정렬 성공!)