【问题标题】:Collections.sort() not sort truely [closed]Collections.sort() 没有真正排序[关闭]
【发布时间】:2018-03-22 10:46:52
【问题描述】:

当我使用 Collections.sort() 时,它没有真正排序, 这是我的代码:

matchedWords.addAll(getIntent().getStringArrayListExtra("matched_words"));
    TextView matchedTexts = findViewById(R.id.textView3);
    matchedTexts.setMovementMethod(new ScrollingMovementMethod());
    String text = "You have " + matchedWords.size() + " words in NewGsl Library";

    matchedTexts.setText(text);
    matchedTexts.append("\n");

    Collections.sort(matchedWords);
    for(String match : matchedWords)
    {
        matchedTexts.append( match.toLowerCase());
        matchedTexts.append("\n");
    }

结果是:

1.ıf 
2.ıf
3.reading
4.a
5.a
6.about
.
.
.

【问题讨论】:

  • 您希望它如何排序?

标签: java android collections


【解决方案1】:

你可以这样做,

 Collections.sort(matchedWords, new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {

            return lhs.compareToIgnoreCase(rhs);
        }
    });

【讨论】:

  • 这是工作!非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2012-05-12
  • 2021-01-05
  • 2020-05-21
  • 2017-05-03
  • 1970-01-01
  • 2016-04-18
  • 2018-10-11
  • 1970-01-01
相关资源
最近更新 更多