範例如下
package test.charles.CollectionSort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class App
{
public static void main( String[] args )
{
List userList = new ArrayList();
//add user name and age
userList.add(new User("user1",19));
userList.add(new User("user2",40));
userList.add(new User("user3",3));
userList.add(new User("user4",27));
userList.add(new User("user5",10));
userList.add(new User("user6",70));
//sort by user age
Collections.sort(userList, new Comparator() {
public int compare(User o1, User o2) {
return o1.getAge().compareTo(o2.getAge());
}
});
//print information
for(User s :userList){
System.out.println("name:" + s.getName() + ",age:" + s.getAge());
}
}
}
程式碼可以至github下載
沒有留言:
張貼留言