Groovy web console

subscribe to the feed Subscribe
to this
site

groovy java sort toSorted dangerous

Published 4 months 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
println("sorted=$sorted")

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