2016-06-17 53 views
2

地圖自定義排序的名單上有地圖的列表,像下面:Groovy的其他列表順序

List listOfMap = [ 
      [ 
       "id": 20914, 
       "objectProp": "document.forms[0].elements[9]", 
       "locator": "dom" 
      ], 
      [ 
       "id": 20916, 
       "objectProp": "//tr[5]/td[2]/select", 
       "locator": "xpath" 
      ], 
      [ 
       "id": 20917, 
       "locator": "label", 
       "objectProp": "Title" 
      ], 
      [ 
       "id": 20919, 
       "objectProp": "document.forms[0].elements['pt1:USma:0:MAnt1:1:pt1:pt_r1:0:AP1:NewPe1:0:pt_r1:0:r1:0:i1:6:selectOneChoice4']", 
       "locator": "dom" 
      ] 
     ] 

我想sort上述listOfMap使用下面定位器的列表順序:

List locatorPosition = ["id", "name", "tagName", "className", "linkText", "partialLinkText", "label", "cssSelector", "xpath", "dom"] 

慾望輸出如下:

[[id:20917, locator:label, objectProp:Title], [id:20916, objectProp://tr[5]/td[2]/select, locator:xpath], [id:20914, objectProp:document.forms[0].elements[9], locator:dom], [id:20919, objectProp:document.forms[0].elements['pt1:USma:0:MAnt1:1:pt1:pt_r1:0:AP1:NewPe1:0:pt_r1:0:r1:0:i1:6:selectOneChoice4'], locator:dom]] 

你有沒有任何想法如何實現這一目標?

回答

1

嘗試:

List listOfMap = [ 
      [ 
       "id": 20914, 
       "objectProp": "document.forms[0].elements[9]", 
       "locator": "dom" 
      ], 
      [ 
       "id": 20916, 
       "objectProp": "//tr[5]/td[2]/select", 
       "locator": "xpath" 
      ], 
      [ 
       "id": 20917, 
       "locator": "label", 
       "objectProp": "Title" 
      ], 
      [ 
       "id": 20919, 
       "objectProp": "document.forms[0].elements['pt1:USma:0:MAnt1:1:pt1:pt_r1:0:AP1:NewPe1:0:pt_r1:0:r1:0:i1:6:selectOneChoice4']", 
       "locator": "dom" 
      ] 
     ] 

def order = ["id", "name", "tagName", "className", "linkText", "partialLinkText", "label", "cssSelector", "xpath", "dom"] 
def sorted = listOfMap.sort { order.indexOf(it.locator) } 
assert sorted.locator == ['label', 'xpath', 'dom', 'dom'] 
+0

由於它工作... :) –

+0

我應該爲這個'ListOfMap = [ [ 「ID」:20624, 「objectProp」: 「#userid」, 「定位器」: 「cssselector」 ], [ 「ID」:20628, 「objectProp」: 「//輸入[1]」, 「定位器」: 「的xpath」 ], [ 「ID」 :20631, 「locator」:「id」, 「objectProp」:「userid」 ] ]' –

+0

這個列表失敗 –