且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按长度排序,然后按字母顺序排序

更新时间:2023-02-06 13:49:34

您应该定义一个Comparator,以便以正确的方式比较两个字符串。在您的情况下,较短的字符串将在较长的字符串之前;如果大小相等 - 顺序是字母。
然后你使用这个比较器进行排序 - 使用 Collections.sort()

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.