加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 对自定义对象的矢量进行排序

发布时间:2020-12-15 02:06:44 所属栏目:Java 来源:网络整理
导读:如何对自定义对象的矢量进行排序并选择要排序的属性? 我确实看到了这个问题回答,但我不太清楚它的排序基于什么.代码示例将优先于“方法论”. Sort a Vector of custom objects public class ItemLocation { String icon; String title; String message; Str
如何对自定义对象的矢量进行排序并选择要排序的属性?

我确实看到了这个问题&回答,但我不太清楚它的排序基于什么.代码示例将优先于“方法论”.

Sort a Vector of custom objects

public class ItemLocation {
    String icon;
    String title;
    String message;
    String subtext;
    String deviceId;
    double latCoords;
    double lngCoords;
    int expiary;
    int id;
    double proximity;
    String locSeen;
}

解决方法

下面是一个允许您按ItemLocation的指定字段排序的示例:

public void sort(final String field,List<ItemLocation> itemLocationList) {
    Collections.sort(itemLocationList,new Comparator<ItemLocation>() {
        @Override
        public int compare(ItemLocation o1,ItemLocation o2) {
            if(field.equals("icon")) {
                return o1.icon.compareTo(o2.icon);
            } if(field.equals("title")) {
                return o1.title.compareTo(o2.title);
            } else if(field.equals("message")) {
                return o1.message.compareTo(o2.message);
            } 
            .
            . fill in the rest of the fields...
            .
            else if(field.equals("locSeen")) {
                return o1.locSeen.compareTo(o2.locSeen);
            } 
        }           
    });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读