Sort()
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.sort();
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple,Mango
Apple,Banana,Mango,Orange
reverse()
<p id="demo1"></p>
<p id="demo2"></p>
<script>
// Create and display an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
// First sort the array
fruits.sort();
// Then reverse it:
fruits.reverse();
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple,Mango
Orange,Mango,Banana,Apple
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo1").innerHTML = points;
points.sort(function(a, b){return a - b});
document.getElementById("demo2").innerHTML = points;
</script>
40,100,1,5,25,10
1,5,10,25,40,100
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
40,100,1,5,25,10
100,40,25,10,5,1
비교 기능
function(a, b){return a - b}
함수가 두 값을 비교할 때 sort()값을 비교 함수로 보내고 반환된 값(음수, 0, 양수)에 따라 값을 정렬합니다.
결과가 음수이면 a가 앞에 정렬 b됩니다.
결과가 양수이면 b가 앞에 정렬 a됩니다.
결과가 0이면 두 값의 정렬 순서가 변경되지 않습니다.
무작위 순서로 배열 정렬
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
points.sort(function(){return 0.5 - Math.random()});
document.getElementById("demo").innerHTML = points;
}
</script>
1,100,10,40,5,25
Fisher Yates shuffle
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
for (let i = points.length -1; i > 0; i--) {
let j = Math.floor(Math.random() * (i+1));
let k = points[i];
points[i] = points[j];
points[j] = k;
}
document.getElementById("demo").innerHTML = points;
}
</script>
Math.max()
<p>The highest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMax(points);
function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
</script>
The highest number is 100
Math.min()
<p>The lowest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMin(points);
function myArrayMin(arr) {
return Math.min.apply(null, arr);
}
</script>
The lowest number is 1
Find Max
function myArrayMax(arr) {
let len = arr.length;
let max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}
Find Min
function myArrayMin(arr) {
let len = arr.length;
let min = Infinity;
while (len--) {
if (arr[len] < min) {
min = arr[len];
}
}
return min;
}
'JavaScript' 카테고리의 다른 글
JavaScript - Date Formats (0) | 2022.11.16 |
---|---|
JavaScript - Array Iteration (0) | 2022.11.15 |
JavaScript - Arrays (0) | 2022.11.15 |
JavaScript - Number Methods (0) | 2022.11.15 |
JavaScript - Template Literals (0) | 2022.11.10 |