티스토리 뷰

Strategy - StringLengthComparator.zip



# StringLengthComparator.java


import java.util.Comparator;


public class StringLengthComparator implements Comparator<String> {


    @Override

    public int compare(String o1, String o2) {

if (o1.length() == o2.length()) {

   return 0;

} else {

   return o1.length() > o2.length() ? 1 : -1;

}

    }

}



# Main.java


public class Main {


    public static void main(String[] args) {

String[] strArray = {"aaa", "bb", "c"};

Arrays.sort(strArray, new StringLengthComparator());

for (String str : strArray) {

   System.out.println(str);

}

    }

}



# 결과


c

bb

aaa


댓글
댓글쓰기 폼
공지사항
Total
407,537
Today
11
Yesterday
19
«   2019/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
글 보관함