Tuesday, September 23, 2008

Sorter

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class Sorter {

/**
* @param args
*/

@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list1= new ArrayList();
Test test= new Test();
test.id =1;
test.name="f";
list1.add(test);

Test test1= new Test();
test1.id =1;
test1.name="c";
list1.add(test1);

Test test2= new Test();
test2.id =1;
test2.name="a";
list1.add(test2);

Test test3= new Test();
test3.id =1;
test3.name="b";
list1.add(test3);






Collections.sort(list1, new Comparator(){

public int compare(Object arg0, Object arg1) {
// TODO Auto-generated method stub
String name1=((Test)arg0).name;
String name2=((Test)arg1).name;
return name1.compareTo(name2);
}

});
System.out.println(list1);
}

}
class Test {
public int id ;
public String name;

}



No comments: