特殊字符、数字开头,及中英文字符排序(类似通讯录)
生活随笔
收集整理的這篇文章主要介紹了
特殊字符、数字开头,及中英文字符排序(类似通讯录)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
近期做項(xiàng)目需要實(shí)現(xiàn)組織用戶(hù)下的個(gè)人用戶(hù)按其名稱(chēng)排序,因此寫(xiě)了個(gè)按字符排序的工具類(lèi)供大家參考
public static void main(String[] args) {String[] arrays = new String[] { "吳彥祖", "lilin", "987明天","yao", "$89ada","_adgq","@%$^117", "房祖名", "光輝女郎", "43165hsk","gg123", "#^&hs", "梅艷芳", "123科比", "tom","Tom", "xiaoming000" };List<String> list = new ArrayList<>();List<List<String>> listGroup = chineseAndEnglishSortByChar(arrays);System.out.println("listGroup.size():"+listGroup.size());for(int i = 0; i<listGroup.size();i++){list.clear();list = listGroup.get(i); // System.out.println("list.size("+i+"):"+list.size());Iterator<String> iterator = list.iterator();while(iterator.hasNext()){System.out.print(iterator.next()+", ");}}}
? ? ? ? 這里按照字符串第一個(gè)字符,使用java原生排序的方式,即特殊字符-->數(shù)字-->英文-->中文,為了能夠?qū)⒂⑽淖址椭形淖址氖鬃帜概帕性谝黄?#xff0c;先將漢字的首字母提取出來(lái)加到該中文字符之前進(jìn)行排序,完成排序后再將該字母去掉。(這里需要用到pinyin4j-2.5.0.jar)
? ? ? ? ?首先,根據(jù)傳入的字符串?dāng)?shù)組返回一個(gè)完成排序的列表
public static List<String> chineseAndEnglishSort(String[] arrays){List<String> list = new ArrayList<>();for (int i = 0; i < arrays.length; i++) {String str = arrays[i];String alphabet = str.substring(0, 1);/*判斷首字符是否為中文,如果是中文便將首字符拼音的首字母和&符號(hào)加在字符串前面*/if (alphabet.matches("[\\u4e00-\\u9fa5]+")) {str = getAlphabet(str) + "&" + str;arrays[i] = str;}}/*設(shè)置排序語(yǔ)言環(huán)境*/Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);Arrays.sort(arrays, com);/*遍歷數(shù)組,去除標(biāo)識(shí)符&及首字母*/for (int i=0;i<arrays.length;i++) {String str=arrays[i];if(str.contains("&")&&str.indexOf("&")==1){arrays[i]=str.split("&")[1];}list.add(arrays[i]);}return list;} public static String getAlphabet(String str) {HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();// 輸出拼音全部小寫(xiě)defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 不帶聲調(diào)defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);String pinyin = null;try {pinyin = (String) PinyinHelper.toHanyuPinyinStringArray(str.charAt(0), defaultFormat)[0];} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}return pinyin.substring(0, 1);}? ? ?? ? ?進(jìn)一步,為了能夠?qū)⑻厥庾址⒆帜搁_(kāi)頭的字符串和中英文字符串按照二十六個(gè)字母區(qū)別開(kāi)來(lái),這里采用返回長(zhǎng)度為28的列表方式,將分類(lèi)排序好的字符串列表返回? public static List<List<String>> chineseAndEnglishSortByChar(String[] arrays){List<String> specCharList = new ArrayList<>();List<String> digitList = new ArrayList<>();List<List<String>> listGroup = new ArrayList<List<String>>();for (int i = 0; i < arrays.length; i++) {String str = arrays[i];String alphabet = str.substring(0, 1);/*判斷首字符是否為中文,如果是中文便將首字符拼音的首字母和&符號(hào)加在字符串前面*/if (alphabet.matches("[\\u4e00-\\u9fa5]+")) {str = getAlphabet(str) + "&" + str;arrays[i] = str;}}/*設(shè)置排序語(yǔ)言環(huán)境*/Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);Arrays.sort(arrays, com);for(int i = 0;i<arrays.length;i++){System.out.println(arrays[i]);}System.out.println();/*遍歷數(shù)組,去除標(biāo)識(shí)符&及首字母*/if(arrays[0].substring(0, 1).matches("[0-9]?")){listGroup.add(specCharList);}else{if((arrays[0].substring(0, 1).matches("^[a-zA-Z]*"))){listGroup.add(specCharList);listGroup.add(digitList);}}for (int i=0;i<arrays.length;) {if((arrays[i].substring(0, 1).matches("[0-9]?"))){while((i<arrays.length)&&(arrays[i].substring(0, 1).matches("[0-9]?"))){digitList.add(arrays[i]);i++;}listGroup.add(digitList);}else if((arrays[i].substring(0, 1).matches("^[a-zA-Z]*"))){for(char ch = 'a';ch <= 'z';ch++){List<String> list = new ArrayList<>();while((i<arrays.length)&&((arrays[i].charAt(0) == ch) || (arrays[i].charAt(0) == (char)(ch-32)))){String str=arrays[i];if(str.contains("&")&&str.indexOf("&")==1){arrays[i]=str.split("&")[1];}list.add(arrays[i]);i++;}listGroup.add(list);}}else{specCharList.add(arrays[i]);i++;if((i>=arrays.length)||((i<arrays.length)&&(arrays[i].substring(0, 1).matches("[0-9]?")))){listGroup.add(specCharList);}}}return listGroup;} ? ? ? ? ? ??測(cè)試代碼public static void main(String[] args) {String[] arrays = new String[] { "吳彥祖", "lilin", "987明天","yao", "$89ada","_adgq","@%$^117", "房祖名", "光輝女郎", "43165hsk","gg123", "#^&hs", "梅艷芳", "123科比", "tom","Tom", "xiaoming000" };List<String> list = new ArrayList<>();List<List<String>> listGroup = chineseAndEnglishSortByChar(arrays);System.out.println("listGroup.size():"+listGroup.size());for(int i = 0; i<listGroup.size();i++){list.clear();list = listGroup.get(i); // System.out.println("list.size("+i+"):"+list.size());Iterator<String> iterator = list.iterator();while(iterator.hasNext()){System.out.print(iterator.next()+", ");}}}
總結(jié)
以上是生活随笔為你收集整理的特殊字符、数字开头,及中英文字符排序(类似通讯录)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 依赖倒置_设计模式之三依赖倒置
- 下一篇: vue服务端渲染 MySQL_Vue 服