Java????????????????SortedSet??????ú??????
???????????? ???????[ 2016/3/4 10:30:53 ] ??????????????? ???????????
??????C??C++???к??????????????????????????ò??ó??????д?????????????????????????λ??????java??????????????????????????java????????????磬?????????????????????Collections.sort(list)??????list?????????????????????Arrays.sort??????????????????????????Arrays.sort?????SortedSet???????????????????????????????????????????????????????????????SortedSet????????????????к????????TreeSet?????????????
???????????????????С???????????????java?????????????С?????compareTo??????????????????????У??????????????Comparable???????????????????implementsComparable???????????????compareTo???????compareTo????????????????????????????????????????????????????е??????????????????????????????????????????Щ?????????????????????????????Comparator?????????????compare?????????л???????equals???????????ù????????Object?????????????????????в???????equals????????????
???????濪????????????????????????????????????????????Comparable?????????????????????????Comparator????????????
???????1????????Comparator????????????????
import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ChineseSortCompare {
@SuppressWarnings("rawtypes")
private final static Comparator CHINA_COMPARE = Collator.getInstance(java.util.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);
}
}
}
???????????????????????????????String??????compare??????????????????????Comparable????????????????????CHINA_COMPARE?????д?????????????????String?????compareTo?????????????????????????????????????????????????
???????2???????????Comparator??????????????
?????????????????????????????????????????????????????UserDO????????????????£?
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;
}
}
???????????????????name??email????????????????name??????????????????????????£?
????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) {
????return cmp.compare(userDO1.getName()?? userDO2.getName());
????}
????}
???????????????????????compare?????????????????????????????????Щ????????????
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) {
sortUserDOArray();
sortUserDOList();
sortUserBySortedSet();
}
private static void sortUserBySortedSet() {
SortedSet<UserDO>userSet = new TreeSet<UserDO>(USER_COMPARATOR);
userSet.add(new UserDO("????" ?? "aaazhangsan@ddd.com"));
userSet.add(new UserDO("????" ?? "ddlisi@dsfds.com"));
userSet.add(new UserDO("????" ?? "ddwangwu@fsadfads.com"));
for(UserDO userDO : userSet) {
System.out.println(userDO.getName());
}
}
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());
}
}
}
??????????Щ????????????????????????????????????????????????μ????????????????????????????????????????????????????????????????????????????????????????β??????????????????????compare?????н???е??????????????????compare??????????????????????0?????????????????????????????????????????0?????????????????????????????????????????????????????????0???????0?????????????????????????????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11