中文排序
1 数组、集合排序
package zys.bd.cg.sheets;
import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class ChineseSortCompare {
@SuppressWarnings("rawtypes")
private final static Comparator CHINA_COMPARE = Collator.getInstance(Locale.CHINA);
public static void main(String []args) {
sortArray();
sortList();
System.out.println("李四".compareTo("张三"));//前者大于后者,则为正数,否则为负数,相等为0
}
@SuppressWarnings("unchecked")
private static void sortList() {
List<String>list = Arrays.asList("张三", "李四", "王五");
Collections.sort(list , CHINA_COMPARE);
for(String str : list) {
System.out.println(str);
}
}
@SuppressWarnings("unchecked")
private static void sortArray() {
String[] arr = {"张三", "李四", "王五"};
Arrays.sort(arr, CHINA_COMPARE);
for(String str : arr) {
System.out.println(str);
}
}
}
2 对象排序
//UserDo
package zys.bd.cg.sheets;
public class UserDO {
protected String name;
protected String email;
public UserDO() {}
public UserDO(String name , String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
//比较器;如果第一个字段相同,再比较第二个字段;
package zys.bd.cg.sheets;
import java.text.Collator;
import java.util.Comparator;
public class UserDOComparator implements Comparator<UserDO>{
Collator cmp = Collator.getInstance(java.util.Locale.CHINA);
@Override
public int compare(UserDO userDO1, UserDO userDO2) {
int sign = cmp.compare(userDO1.getName(), userDO2.getName());
return sign == 0 ? cmp.compare(userDO1.getEmail(), userDO2.getEmail()) : sign;
}
}
//main方法
package zys.bd.cg.sheets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortUserListTest {
private final static UserDOComparator USER_COMPARATOR = new UserDOComparator();
public static void main(String []args) {
sortUserBySortedSet();
sortUserDOList();
sortUserDOArray();
}
private static void sortUserBySortedSet() {
SortedSet<UserDO>userSet = new TreeSet<UserDO>(USER_COMPARATOR);
userSet.add(new UserDO("李四" , "baazhangsan@ddd.com"));
userSet.add(new UserDO("李四" , "adlisi@dsfds.com"));
userSet.add(new UserDO("王五" , "ddwangwu@fsadfads.com"));
for(UserDO userDO : userSet) {
System.out.println(userDO.getName()+":"+userDO.getEmail());
}
}
private static void sortUserDOList() {
List<UserDO>list = Arrays.asList(
new UserDO("张三" , "aaazhangsan@ddd.com"),
new UserDO("李四" , "ddlisi@dsfds.com"),
new UserDO("王五" , "ddwangwu@fsadfads.com")
);
Collections.sort(list , USER_COMPARATOR);
for(UserDO userDO : list) {
System.out.println(userDO.getName());
}
}
private static void sortUserDOArray() {
UserDO []userDOArray = new UserDO[] {
new UserDO("张三" , "aaazhangsan@ddd.com"),
new UserDO("李四" , "ddlisi@dsfds.com"),
new UserDO("王五" , "ddwangwu@fsadfads.com")
};
Arrays.sort(userDOArray , USER_COMPARATOR);
for(UserDO userDO : userDOArray) {
System.out.println(userDO.getName());
}
}
}