转载:https://blog.csdn.net/wangtaocsdn/article/details/71500500

有时候需要对对象列表或数组进行排序,下面提供两种简单方式:

方法一:将要排序的对象类实现Comparable<>接口。

首先,创建学生类,我们将根据学生成绩对学生进行排序:

/**
* 学生类
*/
class Student implements Comparable<Student>{

String name;
int age;
int score;

public Student(String name, int age,int score) {
this.name = name;
this.age = age;
this.score = score;
}

@Override
public int compareTo(Studento) {
// TODO Auto-generated method stub
return this.age - o.age;
}
}

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("大铭", 19, 89));
students.add(new Student("来福", 26, 90));
students.add(new Student("仓颉", 23, 70));
students.add(new Student("王磊", 18, 80));

System.out.println("排序前:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}

// 排序
Collections.sort(students);

System.out.println("排序后:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
}
}

同理,也可以根据对象的其他属性进行排序。

方法二:使用Comparator匿名内部类实现。

还是使用同一个例子,按成绩将学生排序:

/**
* 学生类
*/
class Student {

String name;
int age;
int score;

public Student(String name, int age,int score) {
this.name = name;
this.age = age;
this.score = score;
}
}

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("大铭", 19, 89));
students.add(new Student("来福", 26, 90));
students.add(new Student("仓颉", 23, 70));
students.add(new Student("王磊", 18, 80));

System.out.println("排序前:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}

Collections.sort(students,new Comparator<Student>() {

@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.age-o2.age;
}
});

System.out.println("排序后:");
for (Student student : students) {
System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
}
}
}

也可以实现按对象属性将对象列表排序。

Java对象排序两种方法的更多相关文章

  1. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  2. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  3. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  4. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  5. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  6. WCF生成客户端代理对象的两种方法的解释

    最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...

  7. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

  8. 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)

    Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...

  9. Intent传递对象的两种方法

    Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...

随机推荐

  1. IFC标准 IFCWALLSTANDARDCASE参数说明

    例如: #229= IFCWALLSTANDARDCASE('3_ydjarPr1s9tRASGqIAUD',#41,'\X2\57FA672C5899\X0\:\X2\78165899\X0\240 ...

  2. 业务逻辑:shiro框架的功能实现

    思路:分别在web.xml配置过滤器以及在applicationContext.xml去配置 实现步骤:1.在pom.xml里引入shiro的坐标 2.在web.xml里配置shiro过滤器 3.在a ...

  3. Struts 第一天

    请简述下Struts2 的执行流程. 首先是,启动tomcat服务器,这时候会加载web.xml,当读到filter标签时,会创建过滤器对象.struts2的核心过滤器(StrutsPrepareAn ...

  4. 除了ROS ,机器人自主定位导航还能怎么做?

    博客转载自:https://www.leiphone.com/news/201609/10QD7yp7JFV9H9Ni.html 雷锋网(公众号:雷锋网)按:本文作者科技剪刀手,思岚科技技术顾问. 随 ...

  5. p3163 [CQOI2014]危桥

    传送门 分析 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string ...

  6. SpringMVC @RequestBody 自动转json Http415错误

    转自: http://blog.csdn.net/tiantiandjava/article/details/46125141 项目中想用@RequestBody直接接收json串转成对象 网上查了使 ...

  7. DropDownList判断值是否存在下拉列表中

    //1.值是text string aa= Request.QueryString["CallReason"].ToString();//获取传值 if (DropDownList ...

  8. Dapper --Execute

    Dapper-Execute Ececute是一种可被任何IDbConnection类型的对象调用的扩展方法.它可以执行一次或多次命令, 并返回受影响的行数.此方法通常用于执行存储过程.插入.更新.删 ...

  9. C#数据类型及差异(复习专用)

    一.数据类型 值类型 类型 描述 范围 默认值 bool 布尔值 True 或 False False byte 8 位无符号整数 0 到 255 0 char 16 位 Unicode 字符 U + ...

  10. C#单例---饿汉式和懒汉式

    单例模式: 步骤: 1.定义静态私有对象 2.构造函数私有化 3.定义一个静态的,返回值为该类型的方法,一般以Getinstance/getInit为方法名称 单例模式有懒汉和饿汉,最好使用饿汉 1. ...