Groovy web console

subscribe to the feed Subscribe
to this

groovy java sort toSorted dangerous

Published 1 year ago by Joe with tags dangerous groovy sort cfRuby
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 toSorted=$initial")

// new sorted collection is as expected

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