Hi I have a list which contains elements [daily,monthly,weekly] or [monthly,weekly,daily] or [weekly,daily]
I need to sort the list in such a way that
[daily,monthly,weekly] == [daily,monthly,weekly]
[monthly, weekly, daily] == [daily, monthly, weekly]
[weekly, daily] == [daily, weekly]
Can some one please help me in this
Assuming you mean that you have a list of Strings, then this should work:
customSorter = {
[ 'daily', 'monthly', 'weekly' ].indexOf( it )
}
assert [ 'daily', 'monthly', 'weekly' ].sort( customSorter ) == [ 'daily', 'monthly', 'weekly' ]
assert [ 'monthly', 'weekly', 'daily' ].sort( customSorter ) == [ 'daily', 'monthly', 'weekly' ]
assert [ 'weekly', 'daily' ].sort( customSorter ) == [ 'daily', 'weekly' ]
Or you could do this (to avoid repeatedly creating a List)
customSorter = { a, b, order=['daily','monthly','weekly'] ->
order.indexOf( a ) <=> order.indexOf( b )
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With