Groovy provides number of ways to sort elements of a list
a.
Using sort method
b.
Using Collections.sort method
Using sort method
Groovy provides below overloaded forms of sort method to
sort the elements of list.
public List sort()
public List sort(boolean mutate)
public List sort(Closure closure)
public List sort(boolean mutate, Closure closure)
public List sort(boolean mutate, Comparator comparator)
public List sort()
Sort the elements of a list. If the Collection is a List,
it is sorted in place and returned. Otherwise, the elements are first placed
into a new list which is then sorted and returned - leaving the original
Collection unchanged.
HelloWorld.groovy
numbers = [2, 9, 7, 3, 19, 8] println "Before sorting. numbers: $numbers" sortedNumbers = numbers.sort() println "numbers: $numbers" println "sortedNumbers: $sortedNumbers"
Output
Before sorting. numbers: [2, 9, 7, 3, 19, 8]
numbers: [2, 3, 7, 8, 9, 19]
sortedNumbers: [2, 3, 7, 8, 9, 19]
As you see the output, the elements of the original list
‘numbers’ also sorted.
public List
sort(boolean mutate)
Sorts the element of list. If the Iterable is a List and
mutate is true, it is sorted in place and returned. Otherwise, the elements are
first placed into a new list which is then sorted and returned - leaving the
original Iterable unchanged.
HelloWorld.groovy
numbers1 = [2, 9, 7, 3, 19, 8] numbers2 = [2, 9, 7, 3, 19, 8] println "Before sorting. numbers: $numbers1" println "Before sorting. numbers: $numbers2" sortedNumbers = numbers1.sort(true) println "numbers1: $numbers1" println "sortedNumbers: $sortedNumbers" println "numbers1.is(sortedNumbers): ${numbers1.is(sortedNumbers)}" sortedNumber = numbers2.sort(false) println "\nnumbers2: $numbers2" println "sortedNumbers: $sortedNumbers" println "numbers2.is(sortedNumbers): ${numbers2.is(sortedNumbers)}"
Output
Before sorting. numbers: [2, 9, 7, 3, 19, 8]
Before sorting. numbers: [2, 9, 7, 3, 19, 8]
numbers1: [2, 3, 7, 8, 9, 19]
sortedNumbers: [2, 3, 7, 8, 9, 19]
numbers1.is(sortedNumbers): true
numbers2: [2, 9, 7, 3, 19, 8]
sortedNumbers: [2, 3, 7, 8, 9, 19]
numbers2.is(sortedNumbers): false
public List
sort(Closure closure)
Sorts this Iterable using the given Closure to determine
the correct ordering. If the Iterable is a List, it is sorted in place and
returned. Otherwise, the elements are first placed into a new list which is
then sorted and returned - leaving the original Iterable unchanged
HelloWorld.groovy
countries = ["India", "Indonesia", "Brazil", "America", "Japan"] println "countries : $countries" println "\nSorting countries" countries.sort {it -> it.length()} println "\nCountries by name length : $countries" countries.sort() println "\nCountries by name : $countries"
Output
countries : [India, Indonesia, Brazil, America, Japan]
Sorting countries
Countries by name length : [India, Japan, Brazil,
America, Indonesia]
Countries by name : [America, Brazil, India, Indonesia,
Japan]
public List
sort(boolean mutate, Closure closure)
Sort the iterable using given closure. If the Iterable is
a List and mutate is true, it is sorted in place and returned. Otherwise, the
elements are first placed into a new list which is then sorted and returned -
leaving the original Iterable unchanged.
HelloWorld.groovy
countries = ["India", "Indonesia", "Brazil", "America", "Japan"] println "countries : $countries" println "\nSorting countries by setting mutate to false" sortedCountries = countries.sort (false, {it -> it.length()}) println "sortedCountries : $sortedCountries" println "countries : $countries" println "\nSorting countries by setting mutate to true" sortedCountries1 = countries.sort (true, {it -> it.length()}) println "sortedCountries : $sortedCountries" println "countries : $countries"
Output
countries : [India, Indonesia, Brazil, America, Japan]
Sorting countries by setting mutate to false
sortedCountries : [India, Japan, Brazil, America,
Indonesia]
countries : [India, Indonesia, Brazil, America, Japan]
Sorting countries by setting mutate to true
sortedCountries : [India, Japan, Brazil, America,
Indonesia]
countries : [India, Japan, Brazil, America, Indonesia]
public List
sort(boolean mutate, Comparator comparator)
Sort the list using comparator. If the Iterable is a List
and mutate is true, it is sorted in place and returned. Otherwise, the elements
are first placed into a new list which is then sorted and returned - leaving
the original Iterable unchanged.
HelloWorld.groovy
countries = ["India", "Indonesia", "Brazil", "America", "Japan"] println "countries : $countries" println "\nSorting countries by setting mutate to false" sortedCountries = countries.sort (false, { a, b -> a.length() <=> b.length() } as Comparator ) println "sortedCountries : $sortedCountries" println "countries : $countries" println "\nSorting countries by setting mutate to true" sortedCountries1 = countries.sort (true, { a, b -> a.length() <=> b.length() } as Comparator ) println "sortedCountries : $sortedCountries" println "countries : $countries"
Output
countries : [India, Indonesia, Brazil, America, Japan]
Sorting countries by setting mutate to false
sortedCountries : [India, Japan, Brazil, America,
Indonesia]
countries : [India, Indonesia, Brazil, America, Japan]
Sorting countries by setting mutate to true
sortedCountries : [India, Japan, Brazil, America,
Indonesia]
countries : [India, Japan, Brazil, America, Indonesia]
Using
Collections.sort method
java.util.Collections class provides below methods to
sort the elements of a list.
public static <T extends Comparable<? super
T>> void sort(List<T> list)
public static <T> void sort(List<T> list,
Comparator<? super T> c)
First method sorts the elements in their natural
ordering. Second method sort the elements using the provided comparator.
HelloWorld.groovy
countries = ["India", "Indonesia", "Brazil", "America", "Japan"] println "countries : $countries" println "Sorting the countries using their name length" Collections.sort (countries, { a, b -> a.length() <=> b.length() } as Comparator ) println "countries : $countries"
Output
countries : [India, Indonesia, Brazil, America, Japan]
Sorting the countries using their name length
countries : [India, Japan, Brazil, America, Indonesia]
No comments:
Post a Comment