利用Comparator排序
import java.util.Comparator;
class Studentxx {
private String nameString;
private int age;
public Studentxx(String nameString, int age) {
// super();
this.nameString = nameString;
this.age = age;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Studentxx)) {
return false;
}
Studentxx studentxx = (Studentxx) obj;
if (studentxx.nameString.equals(this.nameString)
&& studentxx.age == this.age) {
return true;
} else {
return false;
}
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Studentxx [nameString=" + nameString + ", age=" + age + "]";
}
}
class StudentComparator implements Comparator<Studentxx> {
public int compare(Studentxx s1, Studentxx s2) {
if (s1.equals(s2)) {
return 0;
} else if (s1.getAge() < s2.getAge()) {
return 1;
} else {
return -1;
}
}
}
public class ComparatorDemo {
public static void main(String[] args) {
Studentxx stu[] = { new Studentxx("von", 20), new Studentxx("lee", 23),
new Studentxx("wong", 29), new Studentxx("cong", 23),
new Studentxx("sun", 39), new Studentxx("chao", 24) };
java.util.Arrays.sort(stu, new StudentComparator());
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i]);
}
}
}
java.util.Arrays.sort(Studentxx[] a, Comparator<? super Studentxx> c):
Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.
- Parameters:
- a the array to be sorted
- c the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
- Throws:
- ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
comparator和comparable两个接口都可以实现相投的排序功能,但是与comparable接口相比,comparator接口是一种补救的做法。
利用Comparator排序的更多相关文章
- POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)
(点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...
- Java之——利用Comparator接口对多个排序条件进行处理
转载自:http://blog.csdn.net/l1028386804/article/details/56513205 膜拜大神··· 一.需求 假设现在有个如此的需求:需要对一个这样的雇员列表进 ...
- 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理
一需求 二实现Comparator接口 三验证排序结果 验证第一条件首先按级别排序级别最高的排在前面 验证第二条如果级别相等那么按工资排序工资高的排在前面 验证第三条如果工资相当则按入职年数排序入职时 ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- Java利用反射排序
前言 Java为我们提供了几种排序得方法,比如Arrays和Collections类,但是前提是数组或者集合中的元素都必须实现Comparable接口,基本的数据类型都已经实现了Comparable接 ...
- [Java] 使用Comparator排序对象
package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- Java8:使用Lambda表达式增强版Comparator排序
学习路上的自我记录-------路好长,就问你慌不慌,大声港,不慌.----jstarseven. 实体类: package com.server.model; /** * Created by js ...
- Comparator 排序 ArrayList 实操练习
package ltb6w; import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;impor ...
- NYOJ 8 一种排序(comparator排序)
一种排序 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都 ...
随机推荐
- 10个Laravel4开发者必用扩展包
Laravel是一个新的基于最新PHP版本号语法,支持IoC等设计模式的高速开发框架.眼下最新版本号为4.2,推荐安装PHP版本号5.5+. 本文列举10个基本软件包,都是开发人员使用Laravel框 ...
- JQuery一句话实现全选/反选
$("#checkAll").click(function () { if (this.checked) { $("input[name='checkbox']& ...
- 搭建C#框架 博文观感
最近刚开始着手做项目,在后期开发的时候遇到不少预期之外的问题,而且工期也超出预算不少.反思了一下,主要是做的项目少,前期需求分析不明朗,当然对于框架也没有意识.凡此种种.当然,遇到问题就要去想办法解决 ...
- C# 与 VB.NET 对比
C# 与 VB.NET 对比 2008-06-20 15:30 by Anders Cui, 1462 阅读, 3 评论, 收藏, 编辑 Table of Contents 1.0 Int ...
- IOS 获取手机各种信息
/手机序列号 NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@&qu ...
- C#调用短信接口(通过简单的工厂模式整合多个短信平台)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- python打包成.exe工具py2exe0-----No such file or directory错误
转自:http://justcoding.iteye.com/blog/900993 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具, ...
- Nginx 拒绝指定IP访问
来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/ 闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...
- c++中多态性、dynamic_cast、父类指针、父类对象、子类指针、子类对象
c++多态性是依靠虚函数和父类指针指向子类对象来实现的.简单来说,父类中定义虚函数,父类指针指向子类对象,父类指针调用函数时调用的就是子类的函数. 父类没有定义虚函数,父类指针指向子类对象时,父类指针 ...
- mysql查询语句理解
看一个查询语句 ,)) as passcount FROM (SELECT b.user,b.full_name,b.user_group From login_log a LEFT JOIN vic ...