본문 바로가기

카테고리 없음

자바스크립트 점수, 우선순위 정렬

 
    let type_score = [
      {
        type: "A",
        score: 30,
        rank: 1,
      },
      {
        type: "B",
        score: 20,
        rank: 2,
      },
      {
        type: "C",
        score: 0,
        rank: 3,
      },
      {
        type: "D",
        score: 10,
        rank: 4,
      },
      {
        type: "E",
        score: 0,
        rank: 5,
      },
    ];


    // 1. 타입별 총점 높은 순 정렬
    // 2. 동률일 경우, A, B, C, D, E 순 정렬
    let type_score_result = type_score.sort(function (a, b) {
      if (a.score < b.score) return 1;
      if (a.score > b.score) return -1;
      if (a.rank > b.rank) return 1;
      if (a.rank < b.rank) return -1;
    });