private JSONArray sortJsonArrayAlphabetically(JSONArray jsonArray, String key) throws JSONException {

        List<JSONObject> jsonValues = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonValues.add(jsonArray.getJSONObject(i));
        }

        Collections.sort(jsonValues, (o1, o2) -> {
            String valA;
            String valB;
            try {
                valA = (String) o1.get(key);
                valB = (String) o2.get(key);
            }
            catch (JSONException e) {
                throw new RuntimeException(e);
            }
            return valA.compareTo(valB);
        });

        return new JSONArray(jsonValues);
    }
This will sort a json array in alphabetical order.
@param jsonArray
@return sorted jsonArray
@throws JSONException
test/common/src/main/java/io/apiman/test/common/json/JsonCompare.java