十五、Collections.sort(<T>, new Comparator<T>() {})针对字符串排序
1、排序对象全是字母组成,可以根据ASCII编码表排序
package com.abcd;
public class Person{
private String name;
private int age;
private int salary;
public Person() {
}
public Person(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
2、排序测试代码
package com.abcd; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class PersonTest {
public static void main(String[] args){
List<Person> people = new ArrayList<>();
people.add(new Person("ZZZ",20,100));
people.add(new Person("aaa",48,109));
people.add(new Person("aa",30,109)); System.out.println(people);
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) { return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase()); }
});
System.out.println(people);
}
}
int comparaTo( )方法底层源码:
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
//首先的比较规则,比较首字母,依次第二位字母,第三位字母等逐个比较
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
//然后字母比较完了后,比较字符串的长度
return len1 - len2;
}
3、运行结果
[Person{name='ZZZ', age=20, salary=100}, Person{name='aaa', age=48, salary=109}, Person{name='aa', age=30, salary=109}]
[Person{name='aa', age=30, salary=109}, Person{name='aaa', age=48, salary=109}, Person{name='ZZZ', age=20, salary=100}]
2、排序目标全是中文,实现首字母排序
package com.abcd; import java.text.Collator;
import java.util.*; public class PersonTest {
public static void main(String[] args){
List<Person> people = new ArrayList<>();
people.add(new Person("博泰",20,100));
people.add(new Person("阿明",48,109));
people.add(new Person("啊",30,109)); System.out.println(people);
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) { Comparator cmp = (Collator.getInstance(Locale.CHINA));
return cmp.compare(o1.getName(), o2.getName()); }
});
System.out.println(people);
}
}
运行结果:
[Person{name='博泰', age=20, salary=100}, Person{name='阿明', age=48, salary=109}, Person{name='啊', age=30, salary=109}]
[Person{name='啊', age=30, salary=109}, Person{name='阿明', age=48, salary=109}, Person{name='博泰', age=20, salary=100}]
十五、Collections.sort(<T>, new Comparator<T>() {})针对字符串排序的更多相关文章
- Collections.sort(List<T> Comparator) 自定义排序
Collections.sort(basicinfoList, new Comparator<MlisBasicinfo>() { @Override public int compare ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 015——数组(十五)sort natsort shuffle natcasesoft array_multisort
<?php /*数组排序函数 * sort natsort shuffle natcasesoft array_multisort */ //sort() 对数组元素进行递增的排序, /*$ar ...
- Python学习日记(十五) collections模块
在内置函数(dict.list.set.tuple)的基础上,collections模块还提供了几个其他的数据类型:Counter.deque.defaultdict.namedtuple和Order ...
- 关于java Collections.sort 排序
public static void main(String[] args) { int[] dd = {12,34,46,123,23,2,35,13,543231,65,5645,57}; Arr ...
- Java提高十五:容器元素比较Comparable&Comparator深入分析
我们经常用容器来存放元素,通常而言我们是不关系容器中的元素是否有序,但有些场景可能要求容器中的元素是有序的,这个时候用ArrayList LinkedList Hashtable HashMap ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...
- Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用
摘要:本文主要介绍Java8 中Arrays.sort()及Collections.sort()中Lambda表达式及增强版Comparator的使用. 不废话直接上代码 import com.goo ...
随机推荐
- WebApp的自动测试工具: protractor和selenium
Protractor是Selenium的扩充,支持Angularjs element(by.css('my-css')).click(); 一.用by的各种Locator定位元素 选中1个元素: el ...
- 如何将composer设置为全局变量?
全局安装是将 Composer 安装到系统环境变量 PATH 所包含的路径下面,然后就能够在命令行窗口中直接执行 composer 命令了. Mac 或 Linux 系统: 打开命令行窗口并执行如下命 ...
- PHP实现大转盘抽奖算法实例
本文主要向大家介绍了PHP语言实现大转盘抽奖算法,通过具体的实例向大家展示,希望对大家学习PHP抽奖有所帮助. 流程:1.拼装奖项数组,2.计算概率,3.返回中奖情况 代码如下:中奖概率 ' v ' ...
- node环境
下载教程:http://www.runoob.com/nodejs/nodejs-install-setup.html 选择版本下载:https://nodejs.org/en/download/ 输 ...
- SQL SERVER 死锁
sp_lock 查看锁表名称 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefr ...
- Suse linux enterprise 11安装时更改磁盘模式为gpt的方法
在进行鸟哥linux基础篇学习时,在"第3.2.2 选择安装模式与开机 -inst.gpt"中,鸟哥用到的CentOS 7需要用指令修改磁盘模式为gpt. 先用键盘选择Instal ...
- 【C++】C++中int与string的相互转换
一.int转string 1.c++11标准增加了全局函数std::to_string: string to_string (int val); string to_string (long val) ...
- entityVo对象与entity对象
java Web项目Service层通用接口和entityVo对象与entity对象转化问题的解决方案 原创 2015年12月17日 15:42:09 标签: java / Service / 通用工 ...
- CSS 背景图像 填充部分元素示例
填充部分元素示例 为某个元素设置CSS规则background-image 属性,则可以做到部分元素有背景颜色. 下面的示例演示如何如何给段落元素加背景. <!DOCTYPE html> ...
- python中re正则表达式
1.re匹配的语法 re.math 从头开始匹配,没有匹配到返回None re.seach 匹配包含,,没有匹配到返回None re.findall 把所有匹配到的字符,以列表的形式返回,没有匹配到返 ...