Collections.sort()没有真正排序

时间:2018-03-22 10:46:53

标签: java android collections

当我使用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
.
.
.

1 个答案:

答案 0 :(得分:2)

你可以这样做,

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

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