title: How can i sort my JSON object based on Key?
body: <p>I am creating a JSON object in which i am adding a key and a value which is an array. The value for both key and value comes from a TreeSet which has data in sorted form. However, when I insert data in my json object, it is stored randomly without any order. 
  This is my json object currently:</p>
  
  <pre><code>{
      "SPAIN":["SPAIN","this"],
      "TAIWAN":["TAIWAN","this"],
      "NORWAY":["NORWAY","this"],
      "LATIN_AMERICA":["LATIN_AMERICA","this"]
  }
  </code></pre>
  
  <p>and my code is:</p>
  
  <pre><code> Iterator&lt;String&gt; it= MyTreeSet.iterator();
  
          while (it.hasNext()) {
              String country = it.next();
              System.out.println("----country"+country);
              JSONArray jsonArray = new JSONArray();
              jsonArray.put(country);
              jsonArray.put("this);
  
              jsonObj.put(country, jsonArray);
          }
  </code></pre>
  
  <p>Is there any way I can store the data into my json object inside the while loop itself?</p>