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>() {})针对字符串排序的更多相关文章

  1. Collections.sort(List<T> Comparator) 自定义排序

    Collections.sort(basicinfoList, new Comparator<MlisBasicinfo>() { @Override public int compare ...

  2. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  3. 015——数组(十五)sort natsort shuffle natcasesoft array_multisort

    <?php /*数组排序函数 * sort natsort shuffle natcasesoft array_multisort */ //sort() 对数组元素进行递增的排序, /*$ar ...

  4. Python学习日记(十五) collections模块

    在内置函数(dict.list.set.tuple)的基础上,collections模块还提供了几个其他的数据类型:Counter.deque.defaultdict.namedtuple和Order ...

  5. 关于java Collections.sort 排序

    public static void main(String[] args) { int[] dd = {12,34,46,123,23,2,35,13,543231,65,5645,57}; Arr ...

  6. Java提高十五:容器元素比较Comparable&Comparator深入分析

    我们经常用容器来存放元素,通常而言我们是不关系容器中的元素是否有序,但有些场景可能要求容器中的元素是有序的,这个时候用ArrayList  LinkedList  Hashtable HashMap ...

  7. Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

    Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...

  8. 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 ...

  9. Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用

    摘要:本文主要介绍Java8 中Arrays.sort()及Collections.sort()中Lambda表达式及增强版Comparator的使用. 不废话直接上代码 import com.goo ...

随机推荐

  1. PhysicalBasedRendering(一)物理篇

    很多人对PBR的理解是存在偏差的,跳不出传统渲染模型的思维圈子,把它理解成一种模拟效果更为精确的算法公式,虽然在某种程度上是对的,但没有看到PBR的本质. PBR是对光在真实世界中与环境交互的一种近似 ...

  2. postgis创建空间数据库,导入shp数据

    使用向数据库中导入数据

  3. Flutter windows环境安装 + 模拟设备 + 项目运行

    目录: 一.JDK安装 1.1.JDK下载 1.2.环境变量配置 1.3.测试 二.ANDROID-SDK安装 2.1.下载 2.2.环境变量配置 三.Flutter安装 3.1.下载 3.2.环境变 ...

  4. eclipse注解模板,实实在在的

    1.可以引用其他codetemplate.xml 2.可以自己配置,很快,就几个主要的,个人建议这个,配一次妥妥的,以后就会了,也不用搭理模板了 3.按照公司要求 拿去https://www.jian ...

  5. JSON.stringify()的深度使用

    在使用JSON.stringify()对JSON数据进行序列化时 1> 如果里面的属性是function,则会被忽略 const data = { a: 'a', fn: funciton() ...

  6. php中上传图片,原生代码

    一.实现图片上传方法一 html的写法: <form action="handle.php" name="form" method="post& ...

  7. Oracle监听已经启动了 sqlplus / as sysdba 仍然报 ERROR:ORA-12560

    我的Oracle是11g的 system 的密码忘记了, 但是scott的密码我还记得而且能登录 . 想以sqlplus /as sysdba 修改system密码 一直在报 ERROR:ORA-12 ...

  8. QMap迭代器

    QMap<int, QString> intToStr; intToStr[] = "test" for (auto iter = intToStr.begin(); ...

  9. The requested URL /xxxx.html was not found on this server

    The requested URL /xxxx.html was not found on this server 的原因是,Apache没有开启重写 apache 打开 httpd.conf 文件找 ...

  10. MySQL(基础技能)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...