Collections.sort not sorting ArrayList as expected [beanshel



我有一些Java代码,我在Beanshell处理器(JMeter)中使用。这段java代码简单而有效。它应该简单地对数字数组列表进行排序,但它给出了奇怪的行为:

// 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等等我不明白这种奇怪行为背后的原因。这可能是因为一些问题与"数组输入数据"?集合。Sort似乎工作得很好;当我自己创建一个示例数组列表时。对此行为和解决方案的任何评论都将不胜感激。谢谢。

不保存String类型的值,而是保存为数字:

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

按字符串排序通过逐个比较每个字符来工作,例如:

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

相关内容

  • 没有找到相关文章