package test;
import java.util.*;
public class SortTest {
private List<Student> lista = new ArrayList<Student>();
/**
* @param args
* @CreateDate:2012-10-14
* @author
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
simpleSort();
SortTest s = new SortTest();
s.listSort();
}
/**
* 字符串排序,基本用不上
* @CreateDate:2012-10-14
* @author
*/
public static void simpleSort(){
String[] strs = {"abb","dfs","adc"};
Arrays.sort(strs);
System.out.println(strs[0]+","+strs[1]+","+strs[2]);
}
/**
* list排序
* @CreateDate:2012-10-14
* @author
*/
public void listSort(){
this.lista.add(new Student("abb"));
this.lista.add(new Student("dfs"));
this.lista.add(new Student("adc"));
Collections.sort(this.lista);
for(Student s:lista){
System.out.println(s.name);
}
//降序
Comparator comp = Collections.reverseOrder();
Collections.sort(this.lista,comp);
for(Student s:lista){
System.out.println("降序:"+s.name);
}
}
}
class Student implements Comparable<Student>{
public String name;
public Student(String name){
this.name = name;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.name.compareTo(((Student)o).name);
}
}
结果:
abb,adc,dfs
abb
adc
dfs
降序:dfs
降序:adc
降序:abb