Already posted questions doesn't have solution for mine because all other solution using POJO field to sort but case is no POJO as request/responses are dynamic json , hence posting I need to perform multi level [with multiple attributes] sorting. The sorting multiple attributes are coming dynamic from input request..
Have created individual Comparator for each dataTypes like String,Numeric, etc
e.g. IntegerComparator
public class IntegerComparator implements <DTOObject> {
@Override
public int compare(DTOObject o1, DTOObject o2){
return o1.getAccountID()-o2.getAccountID();
}
} //since my json response is dynamic. getter method is framed
dynamically based on input request tells which parameter to sort.
StringComparator:-
public class StringComparator implements <DTOObject> {
@Override
public int compare(DTOObject o1, DTOObject o2){
return o1.getActName()-o2.getActName();
}
} // since my json response is dynamic. getter method is framed
dynamically based on input request tells which parameter to sort.
Main Class:-
public class AccountService {
public SearchResponse accountFetch(){
//To do
List <JSONObject> resultList= someObject.fetchAcoount("12345678");
// the resultList is dynamic JSON reponse,
so no setter/getter method cannot be created
Collections.sort(resultList, new IntegerComparatorimplements ());
// how to invoke multi level sorting here as sorting attributes are
dynamic and upto N parameters
}
}
Problem
Since my json response is dynamic, getter method is framed dynamically in each comparator based on input request("responseParameters") tells which parameter to sort.
Now how to build composite comparators in runtime for multi level sorting? How to invoke all comparators for multi level sorting?
"responseParameters" in below sample request tells based on what parameter to sort, sequence of parameter to sort [multi level sort], datatype of that param etc for the json response.
Sample Request:-
{
{
"RequestOject":{
"CUSTID": "100"
},
"responseParameters":[
{
"name":"NAME",
"datatype":"String",
"order":"asc",
"sequence":1
},
{
"name":"SYSTEM",
"datatype":"int",
"order":"asc",
"sequence":2
},
{
"name":"AGE",
"datatype":"int",
"order":"desc",
"sequence":4
},
{
"name":"TITLE",
"datatype":"String",
"order":"desc",
"sequence":3
},
upto... N parameters
]
}
}
Note: sorting attributes are dynamic and upto 1... N paramters
Sample Response:-
"ResponseObject":{
"documents": [
{
"DETAIL":[
{"CUSTID":"789"},
{"ACCT":"SAV"},
{"AGE":"22"}
],
"SRC": {
"SYSTEM":"IB",
"TITLE":"PEM"
},
"SYSDATA":{
"NAME":"MATT"
}
},
{
"DETAIL":[
{"CUSTID":"522"},
{"ACCT":"CUR"},
{"AGE":"40"}
],
"SRC": {
"SYSTEM":"IB",
"TITLE":"CON"
},
"SYSDATA":{
"NAME":"JAMES"
}
}
]
}