I am trying to sort an array of strings according to their length using Arrays.sort()
, but this sorts the strings lexicographically rather than by length. Here is my code:
S = "No one could disentangle correctly"
String W[] = S.split(" ");
Arrays.sort(W);
After sorting :
correctly
could
disentangle
no
one
but what I want is
no //length = 2
one //length = 3
could //length = 4 and likewise
correctly
disentangle
How can I get the above output? Please give answer for JDK 1.7 & JDK1.8.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…