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. centos7部署.net core2.1

    1.centos 7.0及以上服务器 2..NET SDK 安装 2.1 安装 https://www.microsoft.com/net/download/linux-package-manager ...

  2. 检测Tensorflow可用设备(比如:显卡)

    打开python命令行,输入以下命令: python -c "from tensorflow.python.client import device_lib;device_lib.list_ ...

  3. 微信小程序报错:id 属性值格式错误。如不能以数字开头。

    出现这个报错时,相信很多人都排除过自己标签上写的id是否有以数字命名的,如果你排除了发现并没有,但是这个报错还是存在,那么我接下来分享的这个情况或者能报到你 这次我也遇到这个报错,最终找出问题所在 这 ...

  4. 使用plot_importance绘制特征重要性曲线

    代码如下所示: # -*- coding: utf-8 -*- #导入需要的包 import matplotlib.pyplot as plt from sklearn import datasets ...

  5. sql 查询语句的练习

    --1.使用基本查询语句. --(1)查询DEPT表显示所有部门名称. select * from dept; --(2)查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行, ...

  6. Python小知识点(5)--面向对象部分

    面向对象: 世间万物,皆可分类.--------------------手机<--------------某一个分类 世间万物,皆为对象.--------------------我的手机< ...

  7. Django自定义模板标签和过滤器

    1.创建模板库 在某个APP所在目录下新建包templatetags,然后在其中创建存储标签或者过滤器的的模块,名称随意,例如myfilters.py. 在这个模块中编写相关代码. 注意:templa ...

  8. python远程连接windows

    远程连接windows系统     https://blog.51cto.com/ckl893/2145809 import winrm win2008 = winrm.Session('http:/ ...

  9. 快乐!ajax入门(1)

    今天试着默写ajax时出现了神秘的问题,出现如图所示的错误: 百度了一下,说是跨源问题,我以为放在同一个文件夹不也是同源嘛!结果打扰了,属实是弟弟,协议,域名,端口相同的算同源,其他的不是!!! 最后 ...

  10. JS时间戳转时间

    function timestampToTime(timestamp) { S = timestamp, T = new Date(1E3 * S), Format = function(Q){ret ...