简体   繁体   English

在Groovy(SoapUI)中创建属性值列表时,该列表是否会自动排序?

[英]When creating a list of Attribute values in Groovy (SoapUI), does the list get sorted automatically?

Basically, what I am trying is this: 1. From a SOAP API response, get a list of all the attribute values from a specific attribute. 基本上,我正在尝试以下操作:1.从SOAP API响应中,获取特定属性的所有属性值的列表。 This attribute can exist multiple times in the response. 该属性可以在响应中多次存在。 2. Copy this list into a 2nd list, and then sort the 2nd list 3. Compare both the lists to see if the 1st and 2nd lists are identical 2.将此列表复制到第二个列表中,然后对第二个列表进行排序。3.比较两个列表以查看第一和第二个列表是否相同

However, I am facing an issue. 但是,我面临一个问题。 When reading the attribute values from the SOAP API response, and then using the Collections.sort on the 2nd list, it seems to be sorting the 1st list as well. 从SOAP API响应中读取属性值,然后在第二个列表上使用Collections.sort时,似乎也在对第一个列表进行排序。

Example: 例:

<Home>
<ele attr="12"/>
<ele attr="11"/>
<ele attr="13"/>
</Home>

I tried the below 2 things: 我尝试了以下两件事:

Method 1: 方法1:

distances.add(new BigDecimal(Home.ele[i].@'attr'[0]))
log.info distances
distanceSorted = distances
Collections.sort(distanceSorted)
log.info distances

This gives me the output as 11,12,13 这给我的输出为11,12,13

Method 2: 方法2:

distances.add(new BigDecimal(Home.ele[i].@'attr'[0]))
log.info distances
distanceSorted = distances
log.info distances

This gives me the output as 12,11,13 这给我的输出为12,11,13

Any ideas why using the Collections.sort() on the distanceSorted list is affecting the distances list as well? 有什么想法为什么在distanceSorted列表上使用Collections.sort()也会影响距离列表? Also, how can I fix this? 另外,我该如何解决?

When you do this: 执行此操作时:

distanceSorted = distances

You just make both variables reference the same list. 您只需使两个变量引用相同的列表即可。 So when you do 所以当你这样做

Collections.sort(distanceSorted)

It sorts the list, so both variables now reference the sorted list 它对列表进行排序,因此两个变量现在都引用已排序的列表

Replace both those lines with 将这两行替换为

distanceSorted = distances.sort(false)

The false tells the sort method to return a new list instead of sorting the original in place false告诉sort方法返回一个新列表,而不是对原始列表进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM