//把需要比较的对象实现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集合对象根据字段排序的更多相关文章

  1. java实现按对象某个字段排序,排序字段和规则自定义

    @SuppressWarnings({ "unchecked", "rawtypes" }) private <T> void sort(List& ...

  2. List集合对象中的排序,随机显示

    List<User> students = new ArrayList<User>(); User user1 = new User(); user1.setAge(112); ...

  3. Java将list<map>或者list<entity>集合根据指定字段排序

    今天项目中用到了,特记录一下 一. List<Map> 如果 item.get(sortField) 有时间,有数字的时候直接toString(),数组结果的排序结果可能不正确 List& ...

  4. List集合基于某个字段排序

    using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...

  5. java的list集合如何根据对象中的某个字段排序?

    转自:http://blog.csdn.net/wangjuan_01/article/details/51351633 List集合按某个字段排序 package wjtest_01; import ...

  6. JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序

    前言:暑期应该开始了,因为小区对面的小学这两天早上都没有像以往那样一到七八点钟就人声喧闹.车水马龙. 前两篇文章介绍了Collection框架的主要接口和常用类,例如List.Set.Queue,和A ...

  7. List泛型集合对象排序

    本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...

  8. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  9. js对象数组多字段排序

    来源:js对象数组按照多个字段进行排序 一.数组排序 Array.sort()方法可以传入一个函数作为参数,然后依据该函数的逻辑,进行数组的排序. 一般用法:(数组元素从小大进行排序) var a = ...

随机推荐

  1. C++ 拷贝构造函数 和 六大函数

    1.  C++什么时候会调用 拷贝构造函数? a.一个对象作为函数参数,以值传递的方式传入函数体: b.一个对象作为函数返回值,以值传递的方式从函数返回:(实际使用时,会被编译器优化掉) c.一个对象 ...

  2. Java 程序 ——感想

    也许大家也有过这样的经历,我这的是受够了: 我们的专业选修课java课上老师留了一个作业,说做完了这个,就不用参加考试了,侥幸于懒惰的心理带领着我,光荣地接受了这个任务,而且按着老师的要求,不断地完善 ...

  3. 70多G的Kindle电子书合集

    70多个G的电子书. 百度网盘分享了几次被屏蔽了, 360网盘每个文件夹最多只能分享2000个文件,所以只能分成多个文件夹分享. 如果大家知道更好的分享方式,欢迎留言相告,多谢: http://yun ...

  4. Android MVP理解

    Android默认采用的是MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 但是却存在很多问题: 1.这个View对应于布局文件,其实能 ...

  5. Angular JS 学习之服务(Service)

    1.AngularJS中,可以创建自己的服务,或使用内建服务: 2.在AngularJS中,服务是一个函数或对象,可在你的AngularJS应用中使用: AngularJS内建了30多个服务:有个$l ...

  6. DSP using MATLAB 随书示例Example2.8

    x = [3, 11, 7, 0, -1, 4, 2]; nx = [-3:3]; h = [2, 3, 0, -5, 2, 1]; nh = [-1:4]; [y,ny] = conv_m(x,nx ...

  7. 数据库查询Database中的表

    public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...

  8. Java数组课后作业

    1.运行TestArrays.java,了解Arrays中的一些重要方法的用法. Arrays.equals(a 1, a2):判断数组是否相等. int[] b = Arrays.copyOf(a, ...

  9. 【wpf】Path画扇形以及Path的Data属性的理解

    <Path x:Name="PathFillColor" Fill="{TemplateBinding Property=Button.Background}&qu ...

  10. 我的c++学习(2)比较两个数字大小

    #include "stdafx.h" #include<iostream> using namespace std; int max(int i, int j){ / ...