상세 컨텐츠

본문 제목

Strategy Pattern - Comparator #2

Java

by techbard 2016. 7. 25. 14:37

본문

반응형



# Main.java


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;


public class Main {


    public static void main(String[] args) {


List<String> al = new ArrayList<>();

al.add("ab");

al.add("c");


Collections.sort(al, new Comparator<String>() {


   // sorted by string length

   @Override

   public int compare(String o1, String o2) {

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

   return 0;

}


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

   }

});

for (String a : al) {

   System.out.println(a);

}

    }

}



# 결과


ab

c


반응형

관련글 더보기

댓글 영역