如预期Collections.sort不排序的ArrayList [BeanShell中,爪哇,JMeter的] [英] Collections.sort not sorting ArrayList as expected [beanshell, Java, JMeter]

查看:275
本文介绍了如预期Collections.sort不排序的ArrayList [BeanShell中,爪哇,JMeter的]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我正在使用的BeanShell处理器(JMeter的),其中一些Java code。这个Java code是简单而有效。它应该只是排序数字数组列表,但它给奇怪的行为:

I have some Java code which I am using in Beanshell processor (JMeter). This java code is simple and valid. It should simply sort the numeric arraylist but it is giving strange behavior:

// Input data is like below:
   student_id_RegEx_1=13
   student_id_RegEx_11=4
   student_id_RegEx_12=23
   student_id_RegEx_13=24

// CREATE ARRAY LIST AND STORE ELEMENTS IN IT
ArrayList strList = new ArrayList();
for (int i=0;i<25; i++){
strList.add(vars.get("student_id_RegEx_" + String.valueOf(i+1)));
}

// Print the ArrayList created by above method [output is]
vars.putObject("ArrayListBeforeSorting",strList);
ArrayListBeforeSorting=[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 4, 23, 24, 25, 26, 27, 28, 29, 5, 6, 7, 8, 9, 10, 11]


// Sort the ArrayList 
Collections.sort(strList);

//Print the sorted ArrayList [below is output]
vars.putObject("ArrayListAfterSorting",strList);
ArrayListAfterSorting=[10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 4, 5, 6, 7, 8, 9]

观察 28,29,4,5,6,7,8,9,10,11 是在sortedArrayList结束。我期待 4,5,6,7,8,9,10,11,12等我不明白后面这种奇怪的行为的原因。难道是因为一些问题与'阵列输入数据? Collections.sort似乎做工精细;当我创建一个样品的ArrayList自己。此行为和解决方案的任何评论都将AP preciated。谢谢你。

Observe the 28, 29, 4, 5, 6, 7, 8, 9, 10, 11 at end of sortedArrayList. I was expecting 4, 5, 6, 7, 8, 9, 10, 11, 12 and so on I cannot understand the reason behind this strange behavior. Could it be because of some issue with 'array input data'? Collections.sort seems to work fine; when I create a sample arraylist myself. Any comments on this behavior and solution would be appreciated. Thanks.

推荐答案

不需要将String类型的值,并将其保存为数字:

Instead of saving values of type String, save them as numbers:

String strValue = vars.get("student_id_RegEx_" + String.valueOf(i+1));
strList.add(Integer.parseInt(strValue));

排序为字符串的工作原理是比较每个字符,一个接一个,例如:

Sorting as Strings works by comparing each character, one by one, for example:

2 4 5
| | | |
2 2 3 3

2 = 2
4 > 2 - therefore, "245" is "bigger" than "2233"

这篇关于如预期Collections.sort不排序的ArrayList [BeanShell中,爪哇,JMeter的]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆