Groovy web console

subscribe to the feed Subscribe
to this
sort and toSort in groovy (via #groovywebconsole)
tweet this snippet Tweet

sort and toSort in groovy

Published 2 months ago by Joe with tags groovy java sort toSorted dangerous #diffRuby
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def initial = [1,3,5,4]

// sort with closure and create a new object
def sorted = initial.toSorted{ a, b -> a <=> b}

// original collection remains as initial state
println("initial after custom toSort=$initial")

// new sorted collection is as expected

// do a naughty sort
// show that the initial has mutated
println("initial after dangerous sort=$initial")