我試圖理清根據使用Arrays.sort()
其長度的字符串數組,但這種排序字符串按字典而不是長度。這裏是我的代碼:如何排序使用Arrays.sort由長度字符串數組()
S = "No one could disentangle correctly"
String W[] = S.split(" ");
Arrays.sort(W);
排序後:
correctly
could
disentangle
no
one
,但我想要的是
no //length = 2
one //length = 3
could //length = 4 and likewise
correctly
disentangle
我怎樣才能得到上面的輸出?請給出答案JDK 1.7 & JDK1.8。
創建自己的比較。 – matt
@matt所以你的意思是我不能通過Arrays.sort() –
來實現這一點。我的意思是使用Arrays.sort(Object [],Comparator)的2參數版本。 – matt