Search

_0009_배열_정렬_숫자2_sort구현

대분류
STEP00 기초문법
소분류
기초문법_배열

영상

개념

<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>배열 숫자 정렬 - sort() 구현하기</title> </head> <body> <script> /* [개념] sort() 구현하기 */ let arr = [6, 7, 2, 1, 9, 77]; // [오름차순 정렬] /* function check(a, b){ return a - b; } */ // [내림차순 정렬] function check(a, b){ return b - a; } function mySort(arr) { for(let i=0; i<arr.length; i++) { for(let j=i; j<arr.length; j++) { if(check(arr[i], arr[j]) > 0) { let temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } mySort(arr); document.write(arr + "<br>"); // 77,9,7,6,2,1 </script> </body> </html>
Java
복사