English 中文(简体)
Groovy中的数组排序
原标题:Sorting arrays in Groovy

我试图比较groovy中的两个数组。到目前为止,我的尝试得到了好坏参半的回应,因此我向集体寻求建议。

在下面的代码中,我将获取2个REST响应,对它们进行解析,并将Invoice节点下的所有内容放入一个数组中。然后,我进一步限定我的数组,这样我就有了InvoiceID的列表,然后尝试比较两个响应的结果,以确保它们是相同的。

当我比较InvoiceID(Guid)的数组时,它们匹配——这不是我所期望的,因为我的2个响应源之间的发票订单当前不同。

当我对发票ID的数组进行排序时,结果会有所不同。

我怀疑我的代码有问题,但我花了一个小时仔细检查,但无济于事。

任何关于在groovy中对数组进行排序或以下代码的建议都将不胜感激:

gu = new com.eviware.soapui.support.GroovyUtils( context )
def xmlSlurper = new groovy.util.XmlSlurper()

// Setting up the response parameters
def responseSTAGE = xmlSlurper.parseText(context.expand( ${GET Invoices - STAGE#Response} ));
def responseSTAGE2 = xmlSlurper.parseText(context.expand( ${GET Invoices - STAGE2#Response} ));
responseInvoicesSTAGE = responseSTAGE.Invoices
responseInvoicesSTAGE2 = responseSTAGE2.Invoices

def arrayOfInvoicesSTAGE = []
def arrayOfInvoicesSTAGE2 = []

def counter = 0

for (invoice in responseInvoicesSTAGE.Invoice) {
    arrayOfInvoicesSTAGE[counter] = responseInvoicesSTAGE.Invoice[counter].InvoiceID
    //log.info counter+" STAGE"+arrayOfInvoicesSTAGE[counter]
    arrayOfInvoicesSTAGE2[counter] = responseInvoicesSTAGE2.Invoice[counter].InvoiceID
    //log.info counter+" STAGE2"+arrayOfInvoicesSTAGE2[counter]
    counter++
}

log.info arrayOfInvoicesSTAGE
log.info arrayOfInvoicesSTAGE2


def sortedSTAGE = arrayOfInvoicesSTAGE.sort()
def sortedSTAGE2 = arrayOfInvoicesSTAGE2.sort()
log.info sortedSTAGE
最佳回答

顺便说一句,你不能代替:

def arrayOfInvoicesSTAGE = []
def arrayOfInvoicesSTAGE2 = []

def counter = 0

for (invoice in responseInvoicesSTAGE.Invoice) {
    arrayOfInvoicesSTAGE[counter] = responseInvoicesSTAGE.Invoice[counter].InvoiceID
    //log.info counter+" STAGE"+arrayOfInvoicesSTAGE[counter]
    arrayOfInvoicesSTAGE2[counter] = responseInvoicesSTAGE2.Invoice[counter].InvoiceID
    //log.info counter+" STAGE2"+arrayOfInvoicesSTAGE2[counter]
    counter++
}

具有

def arrayOfInvoicesSTAGE  = responseInvoicesSTAGE.Invoice*.InvoiceID
def arrayOfInvoicesSTAGE2 = responseInvoicesSTAGE2.Invoice*.InvoiceID
问题回答

在Groovy中,如果两个数组具有相同数量的元素,并且位于相同位置的每个元素都相等,那么它们就被认为是相等的。您可以通过在Groovy控制台中运行以下代码来验证这一点

Integer[] foo = [1,2,3,4]
Integer[] bar = [4,3,2,1]

assert foo != bar

bar.sort()

assert foo == bar




相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签