Hi everybody,
Following is another simple way of sorting collections using commons-beanutils package:
Assume that we have an object as below:
public class User {
private long userId;
private String userName;
private String password;
}
private long userId;
private String userName;
private String password;
}
And we want to sort based on userName field. We can use BeanComparator class of commons-beanutils package as below to sort the list:
ArrayList list = new ArrayList();
//...
//add User object inside the list.
//...
Collections.sort(list, new BeanComparator("userName"));
Notice that, we passed the name of the field to be sorted, as the parameter, to BeanComparator class constructor. We accomplished sorting of a collection using just one line of code:).
Download commons-beanutils from http://commons.apache.org/beanutils.
Notice that, we passed the name of the field to be sorted, as the parameter, to BeanComparator class constructor. We accomplished sorting of a collection using just one line of code:).
Download commons-beanutils from http://commons.apache.org/beanutils.
Hiç yorum yok:
Yorum Gönder