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 = ...
随机推荐
- 通过SharePoint Designer对SharePoint 2010的Master Page进行自定制
1:需要在对应的SiteCollection 和 Site 中开启Publishing的服务 2:在Designer中创建自己的Master Page,进行对原始v4.master代码进行复制,和修改 ...
- POJ 1625 Censored!(AC自动机+DP+高精度)
Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6956 Accepted: 1887 Descrip ...
- VPS -Digital Ocean -初试以及VPN的搭建
首先恭喜你找到这篇博客,它会带你走出困境. 题外话(请忽略):一直以来想搞一个VPS,终于在自己的刺激下试了一下Digital Ocean,还没有使用很长时间不做太多评论,唯一给我的感觉是各种操作还算 ...
- codeforces733D. Kostya the Sculptor 偏序cmp排序,数据结构hash,代码简化
对于n==100.1,1,2或者1,2,2大量重复的形状相同的数据,cmp函数最后一项如果表达式带等于,整个程序就会崩溃 还没有仔细分析std::sort的调用过程,所以这里不是很懂..,mark以后 ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- 在Windows下快速搭建SVN服务器 VisualSVN
下载https://www.visualsvn.com/server/download/ 1.安装 安装SVN服务器: 安装的时候可以选择http协议还是https协议,http协议速度快一些,而ht ...
- JQ学习(三)-ajax
jQuery - AJAX jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法 ...
- Codeforces Round #355 (Div. 2)-A
A. Vanya and Fence 题目连接:http://codeforces.com/contest/677/problem/A Vanya and his friends are walkin ...
- java发送短信--httpclient方式
最近头让我写个发送短信的java程序检测BI系统,检查数据库是否有异常发送,有则发送短信到头的手机里.这里我直说httpclient方式的get请求方式,并且已经有方式的短信的接口了,所以只要再加上参 ...
- 在 Chrome 中调试 Android 浏览器
最近需要使用 Chrome Developer Tools 调试 Android 浏览器,但是官方指南并不是很好使,经过一番折腾,终于调试成功了,在此把经验分享给需要的朋友. Chrome Devel ...