码迷,mamicode.com
首页 > 编程语言 > 详细

十五、Collections.sort(<T>, new Comparator<T>() {})针对字符串排序

时间:2019-04-03 09:24:12      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:out   组成   代码   math   字符串排序   字母   ascii   .so   import   

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>() {})针对字符串排序

标签:out   组成   代码   math   字符串排序   字母   ascii   .so   import   

原文地址:https://www.cnblogs.com/mason117/p/10646619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!