当我运行应用程序时,排序的ArrayList不显示为已排序

问题描述 投票:2回答:2

我有一个ArrayList,我用compareTo排序。当我运行程序时,我按照我将项目输入列表的顺序得到一个列表,即使我知道我已经对它进行了排序。

我在Collections.sort()上打电话给ArrayList,但没有任何反应。当我做Collections.reverse()时,列表按照我最初输入的顺序排列。

public int compareTo(LeaderPlayers players)
    {
        int compareTo=((LeaderPlayers)players).getScore();
        return this.score-compareTo;
    }

这是我比较和使用Collections.sort()排序列表的方式。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

ArrayList<String> workshopParticipants = new ArrayList<String>();
        workshopParticipants.add("Christine");
        workshopParticipants.add("Marty");
        workshopParticipants.add("Tom");
        workshopParticipants.add("JeanMarie");
        workshopParticipants.add("Daniel");

        Collections.sort(workshopParticipants);

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerViewAdapter(this, workshopParticipants);
        adapter.setClickListener(this);
        mRecyclerView.setAdapter(adapter);

RecyclerView中,参与者出现在Christine,Marty,Tom,JeanMarie,Daniel的顺序中。

我希望它们以他们拥有的点数的顺序出现,我在这里没有显示。我已经清楚地对它进行了排序,但它没有显示为已排序。可能是因为我用过的RecyclerView

java android firebase-realtime-database collections
2个回答
1
投票

你正在排序球员的qazxsw poi名字。你需要对具有String值的qazxsw poi对象进行排序,而不仅仅是它们的名称。假设你有一个班级:

LeaderPlayers

你需要这样做:

score

1
投票

使用Java-8,您可以执行以下操作:

class LeaderPlayers implements Comparable<LeaderPlayers> {
  String name;
  int score;
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.