介绍
通过List集合实现排序功能,功能有年龄和姓名的正序和倒序
这里用到了糊涂工具.
代码
Person.java
public class Person {
private String name; //姓名
private String phone; //手机号码
private int age;
public Person(){}
public Person(String name, String phone, int age) {
super();
this.name = name;
this.phone = phone;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", phone=" + phone + ", age=" + age
+ "]";
}
}
ageComparator.java
import java.util.Comparator;
public class ageComparator implements Comparator<Person>{
public int paixu;//排序变量 1为升序 2为倒叙
/**
* 构造器
*/
public ageComparator() {
}
public ageComparator(int paixu) {
super();
this.paixu = paixu;
}
@Override
public int compare(Person o1, Person o2) {
return compareAge(o1.getAge(),o2.getAge());
}
int compareAge(int age1, int age2){
if(paixu == 1){
return age1 > age2 ? 1 : -1;
}else{
return age2 > age1 ? 1 : -1;
}
}
}
PinYinComparator.java
import java.util.Comparator;
import cn.hutool.extra.pinyin.PinyinUtil;
public class PinYinComparator implements Comparator<Person>{
public int paixu;//排序变量 1为升序 2为倒叙
/**
* 构造器
*/
public PinYinComparator() {
}
public PinYinComparator(int paixu) {
super();
this.paixu = paixu;
}
@Override
public int compare(Person o1, Person o2) {
String pinyin_o1 = PinyinUtil.getPinyin(o1.getName());
String pinyin_o2 = PinyinUtil.getPinyin(o2.getName());
//获取拼音字符数组
char[] charArray1 = pinyin_o1.toCharArray();
char[] charArray2 = pinyin_o2.toCharArray();
return compareName(charArray1,charArray2,0);
}
//递归方法逐个比较名字拼音
int compareName(char[] charArray1,char[] charArray2,int index){
if(paixu == 1){
//控制数组防止索引越界
if(index > charArray1.length - 1 || index > charArray2.length - 1){
return charArray1.length > charArray2.length ? 1 : -1;
}
//当拼音不同时则返回比较结果(递归出口)
if(charArray1[index] != charArray2[index]){
return charArray1[index] > charArray2[index] ? 1 : -1;
}
//拼音相同时让索引++
index++;
//继续比较
return compareName(charArray1,charArray2,index);
}else {
if(index > charArray1.length - 1 || index > charArray2.length - 1){
return charArray1.length > charArray2.length ? -1 : 1;
}
if(charArray1[index] != charArray2[index]){
return charArray1[index] > charArray2[index] ? -1 : 1;
}
index++;
return compareName(charArray1,charArray2,index);
}
}
}
TestPerson.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TestPerson {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请选择排序方式:(1.年龄 2.姓名)");
int numMethod = sc.nextInt();
System.out.println("请选择排序顺序:(1.升序 2.降序)");
int numOrder = sc.nextInt();
List<Person> personSet = new ArrayList<Person>();
personSet.add(new Person("张三","976345678941",12));
personSet.add(new Person("张三丰","34545678941",23));
personSet.add(new Person("扁鹊","6745678941",34));
personSet.add(new Person("陈成","23545678941",56));
personSet.add(new Person("陈宫","3575678941",45));
personSet.add(new Person("张全蛋","36745345941",19));
personSet.add(new Person("张三公","987678941",88));
personSet.add(new Person("赵六","987678942",98));
// Collections.sort(personSet, new PinYinComparator(1));
Collections.sort(personSet, new ageComparator(2));
if(numMethod == 1 && numOrder == 1){
Collections.sort(personSet, new ageComparator(1));
}else if (numMethod == 2 && numOrder == 1){
Collections.sort(personSet, new PinYinComparator(1));
}else if (numMethod == 1 && numOrder == 2){
Collections.sort(personSet, new ageComparator(2));
}else if (numMethod == 2 && numOrder == 2){
Collections.sort(personSet, new PinYinComparator(2));
}
for (Person person : personSet) {
System.out.println(person);
}
}
}
展示效果
请选择排序方式:(1.年龄 2.姓名)
2
请选择排序顺序:(1.升序 2.降序)
2
[2021-12-20 17:09:15] [DEBUG] cn.hutool.log.LogFactory: Use [Hutool Console Logging] Logger As Default.
[2021-12-20 17:09:15] [DEBUG] cn.hutool.extra.pinyin.engine.PinyinFactory: Use [TinyPinyin] Engine As Default.
Person [name=赵六, phone=987678942, age=98]
Person [name=张三公, phone=987678941, age=88]
Person [name=张三丰, phone=34545678941, age=23]
Person [name=张三, phone=976345678941, age=12]
Person [name=张全蛋, phone=36745345941, age=19]
Person [name=陈宫, phone=3575678941, age=45]
Person [name=陈成, phone=23545678941, age=56]
Person [name=扁鹊, phone=6745678941, age=34]