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 = ...
随机推荐
- Android之Adapter用法总结-(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
- user-select
样式详查 http://www.css88.com/book/css/properties/user-interface/user-select.htm 1, user-select: none ...
- LoadRunner 脚本学习 -- 读取文件内容
随便创建个txt文档 输入点内容,例如 读取文件内前N个字符: Action() { long myfile; ; ]; char *filename = "E:\\kkk.txt&quo ...
- Hibernate的持久化类状态
Hibernate的持久化类状态 持久化类:就是一个实体类 与 数据库表建立了映射. Hibernate为了方便管理持久化类,将持久化类分成了三种状态. 瞬时态 transient (临时态):持久化 ...
- 【jQuery 使用】 利用jQuery.prop("outerHTML")获取包含自身在内的HTML元素的HTML代码
jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML")的方式设置. ...
- 自定义ActionBar
/** * 1.创建ActionBar对象getSupportActionBar() * 2.布置自己的ActionBar布局(在res/layout) * 3.把自定义的ActionBar布局加载到 ...
- C语言补码作用
补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...
- Codeforces Round #355 (Div. 2)-A
A. Vanya and Fence 题目连接:http://codeforces.com/contest/677/problem/A Vanya and his friends are walkin ...
- 为什么下载APP,扫描二维码,关注微信公众号,就会送牛奶送小礼品?下载使用量高,会怎样?
以前的老办法是到处贴广告,电视上,广播上各种宣传. 在互联网时代,企业要盈利,除了不断优化升级自己的产品和服务,大量推广宣传产品,还要懂得用户思维.现在有网站,有APP,有微信,有二维码,可以卖产品, ...
- Maven的简单使用,HelloWorld
安装好Maven后就用一个简单的HelloWorld程序来测试一下,体验一下Maven.至于不懂的地方,请查看<Maven实战>书籍. 书籍网址:http://download.csdn. ...