title: Comparing 2 JSONArray
body: <p>I have JSONArrays that needs to be compared which may have child entities inside it not in the same order though like this :</p>
  
  <pre><code>[{ "A" : "IN", "B" : "DL"},{ "A" : "US", "B" : "KA"}] //JSONArray 1
  [{ "A" : "US", "B" : "KA"},{ "A" : "IN", "B" : "DL"}] //JSONArray 2
  </code></pre>
  
  <p>Here is my code.
  Before calling JsonElemnt, I am converting both the JSONArray to String and then passing it to my function to compare :</p>
  
  <pre><code>          //Converting both JSONArray to String JsonArray1Str and JsonArray2Str
            JsonElement jsonElement1 = parser.parse(JsonArray1Str);
            JsonElement jsonElement2 = parser.parse(JsonArray2Str);
            System.out.println(compareJson(jsonElement1, jsonElement2));
  </code></pre>
  
  <p>//compareJson function</p>
  
  <pre><code>  public static boolean compareJson(JsonElement jsonElement1, JsonElement jsonElement2) {
      boolean isEqual = true;
      // Check whether both jsonElement are not null
      if (jsonElement1 != null &amp;&amp; jsonElement2 != null) {
  
        // Check whether both jsonElement are objects
        if (jsonElement1.isJsonObject() &amp;&amp; jsonElement2.isJsonObject()) {
          Set&lt;Entry&lt;String, JsonElement&gt;&gt; ens1 = ((JsonObject) jsonElement1).entrySet();
          Set&lt;Entry&lt;String, JsonElement&gt;&gt; ens2 = ((JsonObject) jsonElement2).entrySet();
          JsonObject json2obj = (JsonObject) jsonElement2;
          if (ens1 != null &amp;&amp; ens2 != null &amp;&amp; (ens2.size() == ens1.size())) {
            // Iterate JSON Elements with Key values
            for (Entry&lt;String, JsonElement&gt; en : ens1) {
              isEqual = isEqual &amp;&amp; compareJson(en.getValue(), json2obj.get(en.getKey()));
            }
          } else {
            return false;
          }
        }
  
        // Check whether both jsonElement are arrays
        else if (jsonElement1.isJsonArray() &amp;&amp; jsonElement2.isJsonArray()) {
          JsonArray jarr1 = jsonElement1.getAsJsonArray();
          JsonArray jarr2 = jsonElement2.getAsJsonArray();
          if (jarr1.size() != jarr2.size()) {
            return false;
          } else {
            int i = 0;
            // Iterate JSON Array to JSON Elements
            for (JsonElement je : jarr1) {
              isEqual = isEqual &amp;&amp; compareJson(je, jarr2.get(i));
              i++;
            }
          }
        }
  
        // Check whether both jsonElement are null
        else if (jsonElement1.isJsonNull() &amp;&amp; jsonElement2.isJsonNull()) {
          return true;
        }
  
        // Check whether both jsonElement are primitives
        else if (jsonElement1.isJsonPrimitive() &amp;&amp; jsonElement2.isJsonPrimitive()) {
          if (jsonElement1.equals(jsonElement2)) {
            return true;
          } else {
            return false;
          }
        } else {
          return false;
        }
      } else if (jsonElement1 == null &amp;&amp; jsonElement2 == null) {
        return true;
      } else {
        return false;
      }
      return isEqual;
  
    }
  </code></pre>
  
  <p>But, I am unable to find where I am going wrong. Could somebody please help me out ?</p>
  
  <p>Edit: Any other approach to compare those two JSONArray are also welcomed.</p>