Java: Sort a list of words by length, then by alphabetical order

后端 未结 5 984
[愿得一人]
[愿得一人] 2021-01-13 18:20

I am told to have a list of words sorted by length, and those that have the same length are sorted by alphabetical order. This is what I have for the method that does that s

5条回答
  •  梦毁少年i
    2021-01-13 19:00

    You can use Java 8's lamba utilities to make concise functions that prevent the clutter of using comparator classes, like so:

    Collections.sort(words, (string1, string2) -> Integer.compare(string1.length(), string2.length());
    

    -Example taken from Effective Java by Joshua Bloch

提交回复
热议问题