recyclerview中的项目排序发生在9上

3ks5zfa0  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(279)

在recyclerview中为我的组件排序,按数字排序。我需要从1到100排序。问题是,如果我输入10,那么它在1之后,代码应该在9之后。

应该是:1 2 3 4 5 6 7 8 9 10 11 12 13
我有:1 10 11 12 13 2 4 5 6 7 8 9
这就是我的代码现在的样子,需要修复什么才能解决我的问题?

private void sortArray_type() {
        Arrays.sort(mArraysNames);
        recyclerViewadapter.notifyDataSetChanged();
    }

    private void sortArrayList_type() {
        Collections.sort(mSkatingRinkScheduleList, (o1, o2) -> o1.get_session_number().compareTo(o2.get_session_number()));
        recyclerViewadapter.notifyDataSetChanged();
    }
zf2sa74q

zf2sa74q1#

您可以按如下方式对数字进行排序,在这里生成一个数字列表,然后用 Collections.shuffle ,并用 Collections.sort :

List<Integer> list = makeSequence(1, 15);
Collections.shuffle(list);

for (int i = 0; i < list.size(); i++)
    Log.d(TAG, "Shuffled ITEM: " + list.get(i));

Collections.sort(list, (o1, o2) -> o1.compareTo(o2));

Log.d(TAG, "===========================================");
for (int i = 0; i < list.size(); i++)
    Log.d(TAG, "Sorted ITEM: " + list.get(i));

public static List<Integer> makeSequence(int begin, int end) {
    List<Integer> ret = new ArrayList<>(end - begin + 1);
    for (int i = begin; i <= end; i++, ret.add(i-1)) ;
    return ret;
}

输出:

2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 11
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 14
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 2
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 8
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 10
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 5
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 13
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 12
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 7
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 6
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 15
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 9
2020-12-28 16:30:42.074 29066-29066/com.zzzz D/LOG_TAG: ITEM: 1
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 3
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 4
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ===========================================
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 1
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 2
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 3
2020-12-28 16:30:42.075 29066-29066/com.zzzz D/LOG_TAG: ITEM: 4
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 5
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 6
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 7
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 8
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 9
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 10
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 11
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 12
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 13
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 14
2020-12-28 16:30:42.076 29066-29066/com.zzzz D/LOG_TAG: ITEM: 15

由于代码中存在一些未知值,因此可以在代码中应用相同的值

eoxn13cs

eoxn13cs2#

当您有带有数值的字符串数组时,就会发生这种情况
你必须使用字符串比较器

Arrays.sort(yourArrayName, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
            }
        });

相关问题