title: Creating Composite Comparator in runtime for Sorting
body: <p><strong>My sample request</strong></p>
  
  <pre><code> {
    "requestModel":{
        "CUSTID": "100"
       },
    "returnParameters":[
      {
       "name":"NETWORK/NETID",
       "datatype":"String",
       "order":"asc",
       "sequence":1
      },
      {
       "name":"INFODATA/NAME",
       "datatype":"String",
       "order":"asc",
       "sequence":1
      },
      {
       "name":"SOURCE/SYSTEM",
       "datatype":"int",
       "order":"asc",
       "sequence":2
      },
     ]
   }
  </code></pre>
  
  <p><strong>Sample Response</strong></p>
  
  <p>Below is my dynamically generated Map format of json response[Response parameters will be different each time based on the request params],</p>
  
  <pre><code>"responseModel":{
    "documents": [
  {
   "NETWORK":[
      {"NETID":"1234"},
      {"ACT":"300"}
     ],
     "SOURCE": {
        "SYSTEM":"50"
       },
     "INFODATA":{
       "NAME":"PHIL"
       }
   },
   {
    "NETWORK":[
      {"NETID":"1234"},
      {"ACT":"300"}
     ],
     "SOURCE": {
        "SYSTEM":"100"
       },
     "INFODATA":{
       "NAME":"PHIL"
       }
    }
   ]
  }
  </code></pre>
  
  <p><strong>Problem Statement</strong></p>
  
  <p>I need to do multi level sorting based on the "returnParameters" in the request <strong>which is dynamic</strong>... 
      "order" indicates ascending (or) descending and sequence indicates the the priority for ordering like (group by in sql query)</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>Map&lt;String,Object&gt; documentList = new HashMap&lt;String,Object&gt;();
  JSONObject jsonObject= new JSONObject(response.getContent());
  </code></pre>
  
  <p>response.getContent() -> is nothing but it contains the above json response in Map format.</p>
  
  <pre><code>Now I converting the map to list of json object
  
  JSONArray jsonArray= (JSONArray)jsonObject.get("documents");
  ArrayList&lt;JSONObject&gt; list = new ArrayList&lt;&gt;();
  for(int i=0;i&lt;jsonArray.length();i++){
   list.add((JSONObject) jsonArray.get(i));
   }
   Collections.sort(list, new ResponseSorter());
   public class ResponseSorter implements Comparator&lt;JSONObject&gt; {
    @Override
    public int compare(JSONObject o1,JSONObject o2){
    String s1= (String)((JSONObject) o1.get("NETWORK")).get("NETID");
    String s2= (String)((JSONObject) o2.get("NETWORK")).get("NETID");
    int i1=Integer.parseInt(s1);
    int i2=Integer.parseInt(s2);
    return i1-i2;
    }
   }
  </code></pre>
  
  <p>I'm stuck here to proceed further. Created one for Integer comparator, .Should I create for each dataType? also
    I need to dynamically construct the composite comparator by parsing the "retunrParameters" , below sample is hard coded, how to create dynamically??</p>
  
  <pre><code>(String)((JSONObject) o1.get("NETWORK")).get("NETID"); -&gt; this should be dynamically framed , since "returnParameters" are also dynamic in nature.[NETWORK &amp; NETID may not be come in another request],so my comparator should be capable enough to frame the keys in runtime
  </code></pre>
  
  <p>Would anyone able to assist me to create composite comparator in runtime for sorting?</p>
  
  <p>NOTE:- Java Pojo cannot be created as the response is dynamic nature</p>