### 將List做排序的方法

List裡頭可能是單一基本型別如int, String等等

`public class StringIntPair{    private String string;    private int integer;    public StringIntPair(String s, int i) {        string = s;        integer = i;    }    protected String getString() {        return string;    }    protected int getInteger() {        return integer;    }    public String toString() {        return string + "\t" + integer;    }}`

`import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class ListSort {    public static void main(String[] args) {        List<StringIntPair> list = new ArrayList<StringIntPair>();        list.add(new StringIntPair("ab", 5));        list.add(new StringIntPair("ca", 3));        list.add(new StringIntPair("aa", 2));        list.add(new StringIntPair("db", 4));        list.add(new StringIntPair("bc", 1));        System.out.println("排序前");        for (Object o:list) {            System.out.println(o);        }        // 依string排序        Collections.sort(list,        new Comparator<StringIntPair>() {            public int compare(StringIntPair o1, StringIntPair o2) {                return o1.getString().compareTo(o2.getString());            }        });        System.out.println();        System.out.println("依string排序");        for (Object o:list) {            System.out.println(o);        }        // 依Integer排序        Collections.sort(list,        new Comparator<StringIntPair>() {            public int compare(StringIntPair o1, StringIntPair o2) {                return o2.getInteger()-o1.getInteger();            }        });        System.out.println();        System.out.println("依Integer排序");        for (Object o:list) {            System.out.println(o);        }    }}`

` 排序前 ab 5 ca 3 aa 2 db 4 bc 1 依string排序 aa 2 ab 5 bc 1 ca 3 db 4 依Integer排序 ab 5 db 4 ca 3 aa 2 bc 1`

1. 針對java.util.Map的Value做排序(1)
2. Java World討論文章[請問一下有關Collections.sort]