???????3?????????UserDO????????UserComparableDO?????????????????
?????????UserDO???±?д?UserComparableDO??
import java.text.Collator;
import java.util.Comparator;
public class UserComparableDO extends UserDO implements Comparable<UserDO> {
public UserComparableDO() {}
public UserComparableDO(String name ?? String email) {
this.name = name;
this.email = email;
}
@SuppressWarnings("rawtypes")
private final static Comparator CHINA_COMPARE = Collator.getInstance(java.util.Locale.CHINA);
@SuppressWarnings("unchecked")
@Override
public int compareTo(UserDO userDO) {
return CHINA_COMPARE.compare(this.getName()?? userDO.getName());
}
}
?????????δ???????????????漲?????Comparator?????????????????????????????Щ??????Commons???????????Σ??????????UserDO???????????д??μ???????????????UserDO????????????????????
??????????????????д???Comparator??????????????????????????????????
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortUserListByComparable {
public static void main(String []args) {
sortUserBySortedSet();
sortUserDOList();
sortUserDOArray();
}
private static void sortUserBySortedSet() {
SortedSet<UserComparableDO>userSet = new TreeSet<UserComparableDO>();
userSet.add(new UserComparableDO("????" ?? "aaazhangsan@ddd.com"));
userSet.add(new UserComparableDO("????" ?? "ddlisi@dsfds.com"));
userSet.add(new UserComparableDO("????" ?? "ddwangwu@fsadfads.com"));
for(UserComparableDO userDO : userSet) {
System.out.println(userDO.getName());
}
}
private static void sortUserDOList() {
List<UserComparableDO>list = Arrays.asList(
new UserComparableDO("????" ?? "aaazhangsan@ddd.com")??
new UserComparableDO("????" ?? "ddlisi@dsfds.com")??
new UserComparableDO("????" ?? "ddwangwu@fsadfads.com")
);
Collections.sort(list);
for(UserComparableDO userDO : list) {
System.out.println(userDO.getName());
}
}
private static void sortUserDOArray() {
UserComparableDO []userDOArray = new UserComparableDO[] {
new UserComparableDO("????" ?? "aaazhangsan@ddd.com")??
new UserComparableDO("????" ?? "ddlisi@dsfds.com")??
new UserComparableDO("????" ?? "ddwangwu@fsadfads.com")
};
Arrays.sort(userDOArray);
for(UserComparableDO userDO : userDOArray) {
System.out.println(userDO.getName());
}
}
}
???????????????????????????????????Comparator?????????????TreeSet???????????????????Щ??????
?????????????????????????????????????????????????????Collections.sort??????
??????????1??Collections.sort(List<T> list)
????public static <T extends Comparable<? super T>> void sort(List<T> list) {
????Object[] a = list.toArray();
????Arrays.sort(a);
????ListIterator<T> i = list.listIterator();
????for (int j=0; j<a.length; j++) {
????i.next();
????i.set((T)a[j]);
????}
????}
???????????????Arrays.sort(a)??????????????????д???list????????????????壬????T?????????????Comparable??????????????????????Collections.sort(list)??????????????list?а?????????????????implements Comparable?????????????????????
??????????????????????????Comparator
??????????2??Collections.sort(List<T> list?? Comparator<? super T> c)
????public static <T> void sort(List<T> list?? Comparator<? super T> c) {
????Object[] a = list.toArray();
????Arrays.sort(a?? (Comparator)c);
????ListIterator i = list.listIterator();
????for (int j=0; j<a.length; j++) {
????i.next();
????i.set(a[j]);
????}
????}
????????????????????????????Arrays.sort????????????????????????Arrays??????????????????????????
??????????3??Arrays.sort(T[]t):
????public static void sort(Object[] a) {
????Object[] aux = (Object[])a.clone();
????mergeSort(aux?? a?? 0?? a.length?? 0);
????}
??????????????ν?????mergeSort?????????????????????ο??????????????????????????????????????????mergeSort??????????????????????????????????????sort???????????????????????????????????Collections.sort(List<T>list)?????????????????????????????????????????????????в????implements Comparable??????????????????????????????????????л????????
??????????4 : Arrays.sort(T[]t?? Comparator<? super T> c)
????public static <T> void sort(T[] a?? Comparator<? super T> c)
????{
????T[] aux = (T[])a.clone();
????if (c==null)
????mergeSort(aux?? a?? 0?? a.length?? 0);
????else
????mergeSort(aux?? a?? 0?? a.length?? 0?? c);
????}
????????mergeSort????????????????????????????Comparator?????????????Comparator????ò?????????????????????????????????????????