【问题标题】:How do I sort a list of string and numbers in numerical order?如何按数字顺序对字符串和数字列表进行排序?
【发布时间】:2021-07-08 09:51:51
【问题描述】:

如何以这样的非正式方式修复 id 的排序:

 1
10
11
..
2
20
21
..

我想打印如下:

1
2
3
..
10
11
..

另外,我的 MainData.getName 包含最多 1000 个 ListArrays 列表的字符串和数字,请参阅我的应用图片链接:

我试过了,结果还是一样

int[] range = IntStream.rangeClosed(1, 1000).toArray();
List<String> rangeList = new ArrayList<>();
for (int number : range) {
    rangeList.add(String.valueOf(number));
}

这是我的 MainActivity

Api api = retrofit.create(Api.class);
    Call<List<MainData>> call = api.getData();
    call.enqueue(new Callback<List<MainData>>() {
        @Override
        public void onResponse (Call<List<MainData>> call, Response<List<MainData>> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(MainActivity.this, response.code(), Toast.LENGTH_SHORT).show();
                return;
            }
            List<MainData> postList = response.body();
            // Filter out any items where "name" is blank or null.
            List<MainData> tempList = new ArrayList<>();
            for(MainData data :postList)
            {

                if(null!= data.getName() && !data.getName().isEmpty()) {

                     //sort by name
                    Collections.sort(tempList, (mainData, t1) -> mainData.getName().compareTo(t1.getName()));

                    int[] range = IntStream.rangeClosed(1, 1000).toArray();
                    List<String> rangeList = new ArrayList<>();
                    for (int number : range) {
                        rangeList.add(String.valueOf(number));
                    }

                    //sort by ListId
                    Collections.sort(tempList, (mainData, t1) -> mainData.getListId().compareTo(t1.getListId()) );

                    tempList.add(data);

                }
            }

            RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(tempList, MainActivity.this);
            recyclerView.setAdapter(recyclerViewAdapter);
        }

        @Override
        public void onFailure (Call<List<MainData>> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

这是我的 Json 数据

public class MainData {

    public String listId, name, id;

    public String getListId () {
        return listId;
    }

    public  String getName () {
        return name;
    }

    public  String getId () {
        return id;
    }
}

【问题讨论】:

    标签: java android json arraylist


    【解决方案1】:

    由于您的 listId 是 String ,因此列表将按字典顺序排序。相反,您可以先将字符串转换为整数,然后对其进行排序。下面是插图。

    Collections.sort(tempList, (mainData, t1) -> Integer.parseInt(mainData.getListId())-Integer.parseInt(t1.getListId());
    

    【讨论】:

    • 兄弟名称是一个字符串,但是当我更改代码时,您给我的应用程序名称正在停止并说:java.lang.NumberFormatException:对于输入字符串:“Item 276”
    • 我也按 id 对其进行了排序,它工作正常,但是当我按名称对其进行排序时,它的说法是 ;java.lang.NumberFormatException:对于输入字符串:“Item 276”,要求是按名称
    • 当然,如果您尝试将“Item 276”转换为整数,它会抛出数字格式异常。
    • 整个列表包含这样的数字:带数字的项目我该怎么办。如果我确实尝试并抓住它会忽略整个排序的事情
    • 谢谢这个答案也有效。非常感谢
    【解决方案2】:
    Collections.sort(tempList, (o1, o2) -> {
        // splitting name with respect to white space and setting the integer part in a local variable to compare 
        Integer n1 = Integer.parseInt(o1.getName().split(" ")[1]);
        Integer n2 = Integer.parseInt(o2.getName().split(" ")[1]);
        return n1.compareTo(n2);
    });
    

    这应该可以解决您的问题。但是,此代码要求 MainData 对象的名称字段应始终为“Item XXX”格式,其中 XXX 为数字。

    【讨论】:

    • 非常感谢它的工作,感谢您的宝贵时间
    • 我如何才能学习所有这些员工兄弟,当我做某事时,我总是卡住
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2012-03-16
    相关资源
    最近更新 更多