I need to develop a simple application for sorting and selecting data according to predefined rules. The application must be able to work with JSON lists of objects of arbitrary structure, select objects that contain keys with certain values, and also sort objects by values using the natural sort order.
I made an application with an "include" rule that accepts a set of key:value pairs to check for matches against entries. How can I sort?
static class DataJSON{
public condition condition;
public ArrayList<data> data;
public class exclude{
public String name;
public Integer rating;
public Boolean disabled;
public String user;
}
public class include{
public String name;
public Integer rating;
public Boolean disabled;
public String user;
}
public class data{
public Boolean disabled;
public Integer rating;
public String user;
public String name;
}
public class condition{
public ArrayList<exclude> exclude;
public ArrayList<include> include;
public ArrayList<String> sort_by;
}
}
static class resultPrepare{
public Boolean disabled;
public Integer rating;
public String user;
public String name;
public resultPrepare(Boolean disabled, Integer rating, String user, String name){
this.disabled = disabled;
this.rating = rating;
this.user = user;
this.name = name;
}
}
static class DataResult{
//public Test.result result;
public static ArrayList<result> result;
public DataResult(ArrayList<result> result){
this.result = result;
}
}
public static class result{
public Boolean disabled;
public Integer rating;
public String user;
public String name;
public result(Boolean disabled, Integer rating, String user, String name){
this.disabled = disabled;
this.rating = rating;
this.user = user;
this.name = name;
}
}
public static void main(String[] args){
String json = "{\"data\": [{\"user\": \"[email protected]\", \"rating\": 20, \"disabled\": false, \"name\": \"Ton1\"},\n"
"{\"user\": \"[email protected]\", \"rating\": 14, \"disabled\": false, \"name\": \"Ton\"},\n"
"{\"user\": \"[email protected]\", \"rating\": 25, \"disabled\": false, \"name\": \"T\"}],\n"
"\"condition\": {\"include\": [{\"name\": \"Ton1\"}],\"exclude\": [{\"disabled\": false, \"name\": \"Tonf\"},{\"disabled\": false, \"name\": \"T\"}], \"sort_by\": [\"rating\"]}}";
Gson gson = new Gson();
DataJSON jsParse = gson.fromJson(json, DataJSON.class);
ArrayList<result> res = new ArrayList<result>();
for(int i = 0; i < jsParse.data.size(); i ){
Boolean flagInclude = false;
Boolean flagExclude = false;
Boolean flagExcludeDisabled = false;
if(jsParse.condition.include != null){
for(int j = 0; j < jsParse.condition.include.size(); j ){
if(jsParse.data.get(i).name != null && jsParse.condition.include.get(j).name != null){
if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.include.get(j).name) || jsParse.condition.include.get(j).name == null){
if(jsParse.data.get(i).disabled == jsParse.condition.include.get(j).disabled || jsParse.condition.include.get(j).disabled == null){
if(jsParse.data.get(i).rating == jsParse.condition.include.get(j).rating || jsParse.condition.include.get(j).rating == null){
if((jsParse.data.get(i).user!= null && jsParse.condition.include.get(j).user != null)|| jsParse.condition.include.get(j).user == null){
if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.include.get(j).user) || jsParse.condition.include.get(j).user == null){
flagInclude = true;
break;
}
}
}
}
}
}
}
}else{
flagInclude = true;
}
if(jsParse.condition.exclude != null){
for(int j = 0; j < jsParse.condition.exclude.size(); j ){
if(jsParse.data.get(i).name != null && jsParse.condition.exclude.get(j).name != null){
if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.exclude.get(j).name) || jsParse.condition.exclude.get(j).name == null){
if(jsParse.data.get(i).disabled == jsParse.condition.exclude.get(j).disabled || jsParse.condition.exclude.get(j).disabled == null){
if(jsParse.data.get(i).rating == jsParse.condition.exclude.get(j).rating || jsParse.condition.exclude.get(j).rating == null){
if((jsParse.data.get(i).user!= null && jsParse.condition.exclude.get(j).user != null)|| jsParse.condition.exclude.get(j).user == null){
if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.exclude.get(j).user) || jsParse.condition.exclude.get(j).user == null){
flagExclude = true;
break;
}
}
}
}
}
}
}
}else{
flagExclude = false;
}
System.out.println("flagInclude: " flagInclude " flagExclude: " flagExclude);
if(flagInclude == true && flagExclude == false){
result r = new result( jsParse.data.get(i).disabled, jsParse.data.get(i).rating , jsParse.data.get(i).user, jsParse.data.get(i).name );
res.add(r);
}
}
DataResult dataResult = new DataResult(res);
String jsParse2 = gson.toJson(dataResult.result);
String outJson = "{\"result\":" jsParse2 "}";
System.out.println(outJson);
}
}
Output: "result":[{"disabled":false,"rating":20,"user":"[email protected]","name":"Ton1"}]}
CodePudding user response:
Look into the java Comparator interface and the sort method of the List interface. With that you can create custom comparators for your 'result' class (btw. class names should always begin upper case).
Example of a name comparator for your case. You'll need to implement the missing ones and choose which one to use depending on your ruleset.
// Add me somewhere in your code
public static class NameComparator implements Comparator<Result>
{
@Override
public int compare(Result o1, Result o2) {
return o1.name.compareTo(o2.name);
}
}
// And in the block where you output the json structure:
DataResult dataResult = new DataResult(res);
DataResult.result.sort(new NameComparator()); // <-- I am new
String jsParse2 = gson.toJson(dataResult.result);
String outJson = "{\"result\":" jsParse2 "}";
System.out.println(outJson);
Hope this helps.