본문 바로가기
웹코딩 배우기/· JavaScript

문자가 있는 숫자 배열 - 숫자 순서, 문자 순서

by 닐기 2022. 5. 19.

See the Pen Untitled by nilgi (@nilgi) on CodePen.

 

• 숫자 순서대로
displayCup();
cup.sort(function(a, b){return a.year - b.year});
displayCup();
function displayCup() {
  document.getElementById("wcup").innerHTML =
  cup[0].type + " " + cup[0].year + "<br>" +
.

.

. }

 


 

• 문자 순서대로
displayCup2();
function myFunction() {
  cup2.sort(function(a, b){
    let x = a.type.toLowerCase();
    let y = b.type.toLowerCase();
    if (x < y) {return -1;}
    if (x > y) {return 1;}
    return 0;
  });
  displayCup2();
}
function displayCup2() {
  document.getElementById("wcup2").innerHTML =
  cup2[0].type + " " + cup2[0].year + "<br>" +
.

.

. }