List集合对象根据字段排序
//把需要比较的对象实现Comparable接口实现compareTo方法
public class Address implements Comparable<Address> {
String country;
String city;
String name;
public Address(String country, String city, String name) {
super();
this.country = country;
this.city = city;
this.name = name;
}
public String toString(){
return "\nname:"+this.name+" city:"+this.city+" country:"+this.country;
}
@Override
public int compareTo(Address o) {
//如果国家不相等,那么直接比较其他字段
if(!this.country.equals(o.country)){
return this.country.compareTo(o.country);
}else if(!this.city.equals(o.city)){
return this.city.compareTo(o.city);
}else{
return this.name.compareTo(o.name);
}
}
}
//测试类
public class ComparableTest {
public static void main(String[] args) {
List<Address> list = new ArrayList<Address>();
Address a1 = new Address("中国", "湖南", "屌丝1");
Address a2 = new Address("中国", "湖北", "屌丝2");
Address a3 = new Address("美国", "纽约", "屌丝3");
Address a4 = new Address("中国", "湖北", "屌丝4");
Address a5 = new Address("中国", "湖南", "屌丝5");
Address a6 = new Address("中国", "广西", "屌丝6");
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
list.add(a5);
list.add(a6);
System.out.println(list);//排序前
Collections.sort(list);
System.out.println(list);//排序后
}
}
}
//打印结果
[ name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝6 ncity:广西 ncountry:中国]
[ name:屌丝6 ncity:广西 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国]
List集合对象根据字段排序的更多相关文章
- java实现按对象某个字段排序,排序字段和规则自定义
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> void sort(List& ...
- List集合对象中的排序,随机显示
List<User> students = new ArrayList<User>(); User user1 = new User(); user1.setAge(112); ...
- Java将list<map>或者list<entity>集合根据指定字段排序
今天项目中用到了,特记录一下 一. List<Map> 如果 item.get(sortField) 有时间,有数字的时候直接toString(),数组结果的排序结果可能不正确 List& ...
- List集合基于某个字段排序
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...
- java的list集合如何根据对象中的某个字段排序?
转自:http://blog.csdn.net/wangjuan_01/article/details/51351633 List集合按某个字段排序 package wjtest_01; import ...
- JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序
前言:暑期应该开始了,因为小区对面的小学这两天早上都没有像以往那样一到七八点钟就人声喧闹.车水马龙. 前两篇文章介绍了Collection框架的主要接口和常用类,例如List.Set.Queue,和A ...
- List泛型集合对象排序
本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- js对象数组多字段排序
来源:js对象数组按照多个字段进行排序 一.数组排序 Array.sort()方法可以传入一个函数作为参数,然后依据该函数的逻辑,进行数组的排序. 一般用法:(数组元素从小大进行排序) var a = ...
随机推荐
- barabasilab-networkScience学习笔记2-图理论
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- 【转】HTML网页中插入视频各种方法
现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.Chrome),支持H.264的(Safari.IE ...
- 【Tomcat】直接启动tomcat时为tomcat指定JDK 而不是读取环境变量中的配置
在windows环境下以批处理文件方式启动tomcat,只要运行<CATALINA_HOME>/bin/startup.bat这个文件,就可以启动Tomcat.在启动时,startup.b ...
- Dialog+NumberPicker
package com.gtercn.myapplication; import android.app.Activity; import android.content.DialogInterf ...
- C#函数与SQL储存过程
一点点小认识作为memo,求指正. C#的函数与SQL的储存过程有很多的相似性, 它们都是一段封闭的代码块,来提高代码的重用性,虽然现在复制粘贴很方便,但是我们在写多个函数的时候频繁的复制粘贴相同的内 ...
- Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移
本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0 1.对信息的传输/ ...
- javascript拾掇
用javascript如何给span赋值呢?一般有两种方法: 1>输出html <body onload="s()"><span id="hell ...
- [转]CentOS下安装伪分布式Hadoop-1.2.1
From: http://blog.csdn.net/yinan9/article/details/16805275 环境:CentOS 5.10(虚拟机下) [root@localhost hado ...
- 解决iPhone中overflow:scroll;滑动速度慢或者卡的问题
在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...
- SCOI 2013 密码 & 乱搞
题意: Fish 是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进.通过翻阅古籍,Fish 得知了这个密码的相关信息:1. 该密 ...