如果是装对象则
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestList {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List l=new ArrayList();
// l.add("asds");
// l.add("asdf");
// l.add("erw");
// Collections.sort(l);
// System.out.println(l);
Person1 p=new Person1();
p.setName("asd");
Person1 p1=new Person1();
p1.setName("gdfs");
l.add(p);
l.add(p1);
Collections.sort(l);
System.out.println(l);
}
}
@SuppressWarnings("rawtypes")
class Person1 implements Comparable
{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
private String pwd;
@Override
public int compareTo(Object o) {
Person1 o1=(Person1)o;
return name.compareTo(o1.name);
}
}
如果是字符串直接就行,因为String实现了comparable |