Groovy列表的地图自定义排序按其他列表顺序

时间:2016-06-17 08:30:20

标签: 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 个答案:

答案 0 :(得分: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']