Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy list.sort by first, second then third elements

Tags:

sorting

groovy

You should be able to iterate through the desired sorting in reverse order:

list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]

list = list.sort{ a,b -> a[2] <=> b[2] }
list = list.sort{ a,b -> a[1] <=> b[1] }
list = list.sort{ a,b -> a[0] <=> b[0] }

assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]

Each should override the previous just enough to keep the combined sorting intact.


You can also chain them in order with the Elvis operator, ?:, which will defer to the next comparison when the previous are equal (and <=> returns 0):

list.sort { a,b -> a[0] <=> b[0] ?: a[1] <=> b[1] ?: a[2] <=> b[2] }

If you want to sort arrays of arbitrary (though homogenous) length, you can use this and it will do it in a single pass:

def list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]

list.sort { a, b -> 
    for (int i : (0..<a.size())) {
        def comparison = (a[i] <=> b[i])
        if (comparison) return comparison
    } 
    return 0
}

assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]

Here is another method using Groovy's Spaceship and Elvis operators:

​def list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]

list.sort { a, b ->
   a[0] <=> b[0] ?: a[1] <=> b[1] ?: a[2] <=> b[2]
}

assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]​

Source: Groovier way of sorting over multiple fields in a list of maps in groovy


you can use kobo-commons' CollectionUtils library.

https://github.com/kobo/kobo-commons/wiki/sort-by-multiple-keys

import org.jggug.kobo.commons.lang.CollectionUtils

CollectionUtils.extendMetaClass()


list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]
list = list.sort{ [ it[0], it[1], it[2] ]} // sort by multiple keys
assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]

list2 = [ [name:"a", age:13], [name:"a",age:15], [name:"b", age:13] ]
list2 = list2.sort{[it.name, it.age] } // sort by name and age
assert list2 == [[name:"a", age:13], [name:"a", age:15], [name:"b", age:13]]