sort words by length and then by alphabetical order

后端 未结 4 1627
既然无缘
既然无缘 2021-01-29 06:22

Below you can see my code. It reads words from dictionary and copy words that match specific patern to test.txt. My qusetion is how to sort words in test.txt first by LENGTH and

4条回答
  •  后悔当初
    2021-01-29 07:01

    You should define a Comparator, so that it compares the two strings in a right manner. In your case, the shorter string will go prior to a longer string; if sizes are equal - the order is alphabetic. Then you use this Comparator to do the sorting - use Collections.sort() for it.

提交回复
热议问题