Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun JSONArray.sort(keyName: String): JSONArray {
val sortedJsonArray = JSONArray()
val jsonValues = arrayListOf<JSONObject>()
for (a in 0 until length())
jsonValues.add(getJSONObject(a))
jsonValues.sortWith { a, b ->
val valA = a.getStringSafe(keyName) ?: a.get(keyName) as String
val valB = b.getStringSafe(keyName) ?: b.get(keyName) as String
valA.compareTo(valB, true)
}
for (k in 0 until length())
sortedJsonArray.put(jsonValues[k])
return sortedJsonArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment