플러터 한글 오름차순 정렬

Vincent·2022년 1월 6일
0
// sortedList는 List<ProfileModle>이다.

sortedList
      ..sort((a, b) {
        if (a.name!.substring(0, 1).codeUnits[0] !=
            b.name!.substring(0, 1).codeUnits[0]) {
          return a.name!
              .substring(0, 1)
              .codeUnits[0]
              .compareTo(b.name!.substring(0, 1).codeUnits[0]);
        } else if (a.name!.substring(1, 2).codeUnits[0] !=
            b.name!.substring(1, 2).codeUnits[0]) {
          return a.name!
              .substring(1, 2)
              .codeUnits[0]
              .compareTo(b.name!.substring(1, 2).codeUnits[0]);
        } else {
          return a.name!
              .substring(2, 3)
              .codeUnits[0]
              .compareTo(b.name!.substring(2, 3).codeUnits[0]);
        }
      });

3번째 글자까지 정렬.
그 이상으로 정렬하고 싶다면 else 대신 else if + substring(3,4)... 으로 이어나가면 됨.

내림차순은 앞의 (a, b)부분을 (b,a)으로 바꾸면 간단히 변경 가능.

profile
Flutter 열심히 파보고 있습니다만 연장이 썩 좋지 않네요ㅠ

0개의 댓글