ajayramesh
ajayramesh

Reputation: 3784

How to sort json data in groovy in alphabetic order?

I want to sort an json data which is like this

def json = []
for ( int i=10;i>1;i--){

    if (i==10 || i==9 ){
         json << [ name:"xyz",
            id:i
        ]
    }else 
    if (i==8 || i==7 ){
          json << [ name:"abc",
             id:i
            ]
    }

}
// def jsondata = [success:true, rows:json]

def jsondata = [success:true, rows:json.sort(false) { it.name }]

print jsondata​
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.lang.Boolean, com.cs.AdminController$_closure15_closure83) values: [false, com.cs.controllers.AdminController$_closure15_closure83@3e020351]
Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size()

I want that data to be sorted alphabetic order ascending or descending

above one is working in a groovy console but not in my program , do i need to add something else like lib ?

Upvotes: 0

Views: 3492

Answers (1)

tim_yates
tim_yates

Reputation: 171114

Your output format seems to have no similarity to your code you posted

Also, your code you posted cannot just be run by someone trying to answer this question.

So this will be an educated guess...

Try:

def jsondata = [success:true, rows:json.sort(false) { it.name }, total:totalCount]

If you're using groovy from way back in the day for some unknown reason, then just drop the false, but beware as this will mutate your json list...

def jsondata = [success:true, rows:json.sort { it.name }, total:totalCount]

Upvotes: 1

Related Questions