title: Java application for sorting and selecting
body: <p>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.</p>
  <p>I made an application with an &quot;include&quot; rule that accepts a set of key:value pairs to check for matches against entries. How can I sort?</p>
  <pre><code>
      static class DataJSON{
  
          public condition condition;
  
          public ArrayList&lt;data&gt; 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&lt;exclude&gt; exclude;
              public ArrayList&lt;include&gt; include;
              public ArrayList&lt;String&gt; 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&lt;result&gt; result;
          public DataResult(ArrayList&lt;result&gt; 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 = &quot;{\&quot;data\&quot;: [{\&quot;user\&quot;: \&quot;mike@mail.com\&quot;, \&quot;rating\&quot;: 20, \&quot;disabled\&quot;: false, \&quot;name\&quot;: \&quot;Ton1\&quot;},\n&quot; +
                  &quot;{\&quot;user\&quot;: \&quot;greg@mail.com\&quot;, \&quot;rating\&quot;: 14, \&quot;disabled\&quot;: false, \&quot;name\&quot;: \&quot;Ton\&quot;},\n&quot; +
                  &quot;{\&quot;user\&quot;: \&quot;john@mail.com\&quot;, \&quot;rating\&quot;: 25, \&quot;disabled\&quot;: false, \&quot;name\&quot;: \&quot;T\&quot;}],\n&quot; +
                  &quot;\&quot;condition\&quot;: {\&quot;include\&quot;: [{\&quot;name\&quot;: \&quot;Ton1\&quot;}],\&quot;exclude\&quot;: [{\&quot;disabled\&quot;: false, \&quot;name\&quot;: \&quot;Tonf\&quot;},{\&quot;disabled\&quot;: false, \&quot;name\&quot;: \&quot;T\&quot;}], \&quot;sort_by\&quot;: [\&quot;rating\&quot;]}}&quot;;
          Gson gson = new Gson();
          DataJSON jsParse = gson.fromJson(json, DataJSON.class);
          ArrayList&lt;result&gt; res = new ArrayList&lt;result&gt;();
          for(int i = 0; i &lt; jsParse.data.size(); i++){
              Boolean flagInclude = false;
              Boolean flagExclude = false;
              Boolean flagExcludeDisabled = false;
              if(jsParse.condition.include != null){
                  for(int j = 0; j &lt; jsParse.condition.include.size(); j++){
                      if(jsParse.data.get(i).name != null &amp;&amp; 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 &amp;&amp; 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 &lt; jsParse.condition.exclude.size(); j++){
                      if(jsParse.data.get(i).name != null &amp;&amp; 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 &amp;&amp; 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(&quot;flagInclude:  &quot; + flagInclude + &quot;  flagExclude:  &quot; + flagExclude);
              if(flagInclude == true &amp;&amp; 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 = &quot;{\&quot;result\&quot;:&quot; + jsParse2 + &quot;}&quot;;
          System.out.println(outJson);
      }
  }
  </code></pre>
  <p>Output: &quot;result&quot;:[{&quot;disabled&quot;:false,&quot;rating&quot;:20,&quot;user&quot;:&quot;mike@mail.com&quot;,&quot;name&quot;:&quot;Ton1&quot;}]}</p>