String[]数组排序

1. String[] 字典顺序排序:

String[] strs = {"a", "d", "c", "f", "e", "g", "h", "b"};
		
Arrays.sort(strs);
/*
    // 根据需要重写 Comparator 
    Arrays.sort(strs, new Comparator<String>(){
	  public int compare(String a, String b) {
		 return a.compareTo(b);
	  }
   });
*/		
for(String s : strs)
{
	System.out.print(s+" ");
}

// 输出
// a b c d e f g h 

 

 

2. String[] 存放数字排序:

String[] strs = {"3", "2", "5", "10", "1", "6", "32", "85"};

// 根据需要重写 Comparator 		
Arrays.sort(strs, new Comparator<String>(){
	public int compare(String a, String b) {
		return (Integer.parseInt(a) > Integer.parseInt(b) ? 1 : -1);
	}
});
		
for(String s : strs)
{
	System.out.print(s+" ");
}

// 输出
// 1 2 3 5 6 10 32 85 

你可能感兴趣的:(C++,c,C#,F#)