Note: by default, the priority of Chinese characters is 5 (the first letter of Chinese Pinyin for comparison of Chinese characters), the priority of characters is 4 (except for. Characters), the priority of uppercase letters is 3, the priority of lowercase letters is 2, and the priority of numbers is 1.
Direct insertion sorting algorithm is adopted. In addition, an external jar package jpinyin-1.1.8.jar is used Download address . At the beginning, please type import com.github.stuxuhai.jpinyin. *;
First, we need to be able to compare the size of strings before we can use the sorting algorithm.
public static Boolean compare(String one,String two){//Compare two string sizes one large returns true otherwise false char[] ones=one.toCharArray();//Convert string to character array char[] twos=two.toCharArray(); int number; if(ones.length>twos.length) { number=twos.length; }else{ number=ones.length; } for(int i=0;i<number;i++){ int grade_one=getGrade(ones[i]);//For characters, according to the priority set at the beginning of acquisition, the same priority is compared with different priorities to obtain the size directly int grade_two=getGrade(twos[i]); //System.out.println(grade_one); / / output respective grades //System.out.println(grade_two); if(grade_one>grade_two) { return true; } else{ if(grade_one<grade_two) { return false; } else{//If the grades are equal, continue to compare within each grade int one_num=ones[i]; int two_num=twos[i]; if(grade_one==5){//When all levels are 5, they are all Chinese characters one_num=get_First_Pinyin(ones[i]); two_num=get_First_Pinyin(twos[i]); if(one_num<two_num) { return true; }else{ if(one_num>two_num) { return false; } else{ continue; } } }else{ if(grade_one==4){//When the level is all 4, it means all characters, but by default. Characters are larger than other characters if(ones[i]=='.') { return true; } else{ if(twos[i]=='.') { return false; }else{ continue; } } }else{//When the level is 1,2,3, the comparison is continued, and the same comparison method is the size of the comparison code if(one_num<two_num) { return true; }else{ if(one_num>two_num) { return false; } else{ continue; } } } } } } } return true; } public static int get_First_Pinyin(char c)//Get the first letter of Pinyin of Chinese characters { String[] pinyins=PinyinHelper.convertToPinyinArray(c, PinyinFormat.WITHOUT_TONE); char[] result=pinyins[0].toCharArray(); //System.out.println(pinyins[0]); / / output the whole Pinyin //System.out.println(result[0]); / / enter the first letter of the Pinyin int nums=result[0]; return nums; } public static int getGrade(char c){//Get character priority int i=c; if (i>=19968&&i<=40869) {return 5;} else{ if(i>=48&&i<=57) { return 1; }else{ if(i>=97&&i<=122) { return 2; }else{ if(i>=65&&i<=90) { return 3; } else{ return 4; } } } } }
In the second step, after we can compare the strings, we can use our direct insertion sorting algorithm happily.
public static String[] order(String[] old)//For an unsorted array, return a sorted array { String news[]=new String[old.length]; news[0]=old[0]; int n; for(int i=1;i<old.length;i++) { n=1; while(compare(old[i],news[i-n])) { if(i-n==0) { n=n+1; break; } n++; } int temp1=i; String temp2=old[i]; for(int j=1;j<n;j++) { news[i]=news[i-1]; i=i-1; } news[i]=temp2; i=temp1; } return news; } }
Step three, let's see the result
public static void main(String[] args) { File f=new File("s:\\hi"); String str[]=f.list();//Here I read a directory on my computer String[] news=order(str); System.out.println("----=Before sorting-----"); for(int i=0;i<str.length;i++) { System.out.println(str[i]); } System.out.println("----=After sorting-----"); for(int i=0;i<news.length;i++) { System.out.println(news[i]); } }
This is the result before and after Window sorting. If it is Linux, the result will be more significant, because the directory list of Linux obtained directly from Java is really messy.