Sorting List in Groovy Language

//Sort Object by its Field
// Create a object under file name Student.groovy
class Student{
    int id
    String name
}
 
//Explanation
def studentList = [ new Student(id: 2, name: 'Beta'),
            new Student(id: 1, name: 'Charley'),
            new Student(id: 3, name: 'Alpha')
          ]
          
//output will be Charley, Beta, Alpha
studentList.sort{ it.id }
studentList.each{print it.name+' '}
println() 
//output will be Alpha, Beta, Charley
studentList.sort{ it.name }
studentList.each{print it.name+' '}           

你可能感兴趣的:(object,sort,groovy)