java字典排序
❶ 如何對Map進行字典排序java
//根據 Map的key進行字典排序
Map<String, String> parameterMap = new HashMap<String, String>();
List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(parameterMap.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
//升序排序
public int compare(Entry<String, String> o1,
Entry<String, String> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
❷ java緙栫▼錛氫粠閿鐩樿緭鍏ュ瓧絎︿覆錛屽苟鎸夊瓧鍏擱『搴忓掑簭鎵撳嵃
importjava.util.ArrayList;
importjava.util.Scanner;
publicclassJudgeString{
publicstaticvoidmain(String[]args){
//鎺ュ彈閿鐩樿緭鍏
Scannerin=newScanner(System.in);
//淇濆瓨杈撳叆鏁版嵁鐨凩ist
ArrayList<String>list=newArrayList<String>();
System.out.println("璇瘋緭鍏ュ瓧絎︿覆錛岃緭鍏モ渆nd鈥濅負緇撴潫錛");
//鑾峰彇姣忔¤緭鍏ョ殑瀛楃︿覆
Stringtemp=in.nextLine();
//鑾峰彇杈撳叆鐨勬墍鏈夊瓧絎︿覆
while(!temp.equals("end")){
list.add(temp);
temp=in.nextLine();
}
//璋冪敤鎺掑簭鍑芥暟
sort(list);
//杈撳叆緇撴灉
System.out.println("鎺掑簭鍚庣殑緇撴灉涓猴細");
for(inti=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
//鎺掑簭鍑芥暟錛堝啋娉★級
publicstaticvoidsort(ArrayList<String>list){
Stringtemp="";
for(inti=0;i<list.size();i++){
for(intj=0;j<list.size()-1;j++){
if(list.get(i).compareToIgnoreCase(list.get(j))>0){
temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
}
}
}
}
宸茬粡璋冭瘯榪囦簡錛岃偗瀹氳兘榪愯岋紝鏈変笉鎳傚緱鍙浠ラ棶
❸ JAVA按著字典順序排序
如果同時有 new String[] { "ye", "yel", "yellow" };你的排序是ye,yel,yellow
但是如果new String[] { "yel", "ye", "yellow" };排序就變成 排序後:yel ye yellow
❹ Java怎麼按照漢字字典順序給字元串排序
//按照中文第一個字母派燃弊升序排列的實現x0dx0aimport java.text.Collator; x0dx0aimport java.util.Arrays; x0dx0aimport java.util.Comparator; x0dx0apublic class test { x0dx0a /** x0dx0a * @param args x0dx0a */ x0dx0a public static void main(String[] args) { x0dx0a // TODO Auto-generated method stub x0dx0a Comparator