【发布时间】:2017-03-16 13:02:19
【问题描述】:
我有来自 json 文件的整数列表,并打印该完整列表。对该列表进行排序后,它以不同的方式打印
List<Integer> list = new ArrayList<Integer>(keys);
System.out.println("list is :" + list);
Collections.sort(list);
System.out.println("after sorting is:"+list);
o/p 列表:
[338, 339, 332, 333, 159, 330, 158, 331, 157, 336, 156, 337, 155,etc...0,1,14]
而不是在0,1,2 等中排序......它按以下方式排序:
排序后是:
[0, 1, 10, 100, 101, 102, 103, 104, 105,etc..]
代码:
response = httpUtil.getResponse(aperture_url);
parser = new JSONParser();
object = parser.parse(((String) response));
jsonObject = (JSONObject) object;
aperture_details = (JSONObject) jsonObject.get("response");
historic_price = (JSONObject) aperture_details.get("historic_price");
System.out.println("historic_price:" + historic_price);
keys = historic_price.keySet();
List<Integer> list = new ArrayList<Integer>(keys);
System.out.println("list is :" + list);
Collections.sort(list);
System.out.println("after sorting is:"+list);
【问题讨论】:
-
原来的列表里有2吗?
-
不是0、1、10、100、101、102、103、104、105 ....升序吗?
-
为了澄清 OP 的观点,我相信他们的意思是它的排序更像
List<String>可能排序。0,1,10,11,2,21,22,3 -
请提供minimal reproducible example - 如果不亲自复制,我很难相信这里的行为。
-
我无法重现您的错误,至少当列表元素确实是
Integers 时,如列表的类型参数所示。如果您提供 minimal reproducible example 来演示问题,那么我怀疑我们将能够告诉您您的元素 不是,实际上是Integers,而是Strings,并且您已关闭或忽略编译器的类型安全警告。
标签: java