加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

按照长度排序数组,然后按字母顺序排列

发布时间:2020-12-14 05:32:19 所属栏目:Java 来源:网络整理
导读:如何按字母顺序排列数组? 我有一个名单上的数字,我正在得到: Something1 Something10 Something2 Something3 而我想得到: 东西1东西2东西10 解决方法 public class MyComparator implements ComparatorString{ @Override public int compare(String o1,St
如何按字母顺序排列数组?

我有一个名单上的数字,我正在得到:

Something1
Something10
Something2
Something3

而我想得到:

东西1东西2东西10

解决方法

public class MyComparator implements Comparator<String>{
    @Override
    public int compare(String o1,String o2) {  
      if (o1.length() > o2.length()) {
         return 1;
      } else if (o1.length() < o2.length()) {
         return -1;
      }
      return o1.compareTo(o2);
    }
}

然后使用:

Collections.sort(yourList,new MyComparator());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读