在一个字段排序通用的ArrayList? [英] Sorting generic ArrayList on a field?

查看:120
本文介绍了在一个字段排序通用的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts-based-on-name\">Sorting基于通讯录的ArrayList的名字吗?结果
  排序列表&LT; T&GT;通过特定规则的对象?


是否有一个排序的ArrayList通用的方式蚂蚁?我有一个PlayerBean,包括拳头名和统计信息我想通过名字任何想法进行排序呢?随着一个String数组是简单的Collections.sort(名单)...

 公共类PlayerBean {
    私人字符串名称;
    私人诠释STAT1;
    私人双STAT2;
    私人双STAT3;
    私人双STAT4;        ....
 }


解决方案

使用同样的方法用一个专门的比较:

 新的比较&LT; PlayerBean&GT;(){       @覆盖
       公众诠释比较(PlayerBean O1,O2 PlayerBean){
           返回o1.getName()的compareTo(o2.getName())。
       }
 }

Possible Duplicate:
Sorting an ArrayList of Contacts based on name?
Sort List<T> of objects by a particular rule?

Is there ant way to sort a generic ArrayList? I have a PlayerBean that includes fist name and stats I want to sort it by first name any idea? With a String array it was simple Collections.sort(list)...

 public class PlayerBean {
    private String name;
    private int stat1;
    private Double stat2;
    private Double stat3;
    private Double stat4;

        ....
 }

解决方案

Use the same method with a specialized comparator:

 new Comparator<PlayerBean>() {

       @Override
       public int compare(PlayerBean o1, PlayerBean o2) {
           return o1.getName().compareTo(o2.getName());
       }                
 }

这篇关于在一个字段排序通用的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆