Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sorting JSONArray in java

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 16.1k
    Comment on it

    Hello Guys

        Here, I am writing the code for sorting JSONArray in java technology.
    we can sort jsonarray in ascending or in descending order according to our requirement by changing pattern as below.

     

    private static final Pattern PATTERN = Pattern.compile("(\\D*)(\\d*)");

     

    Following sortJson(jsonArray,String type) method sort the json array, in which we need to pass JSONArray object and key name(i.e String type) of JSONObject.
        

    1. private static final Pattern PATTERN = Pattern.compile("(\\D*)(\\d*)");
    2. public static JSONArray sortJson(JSONArray jsonArraylab,String type)
    3. {
    4. final String value=type;
    5. JSONArray sortedJsonArray = JSONFactoryUtil.createJSONArray();
    6. List<JSONObject> jsonValues = new ArrayList<JSONObject>();
    7. for (int i = 0; i < jsonArraylab.length(); i++) {
    8. jsonValues.add(jsonArraylab.getJSONObject(i));
    9. }
    10. Collections.sort( jsonValues, new Comparator<JSONObject>() {
    11. //You can change "Name" with "ID" if you want to sort by ID
    12. private final String KEY_NAME = value;
    13. @Override
    14. public int compare(JSONObject a, JSONObject b) {
    15. String valA = new String();
    16. String valB = new String();
    17. valA = (String) a.getString(KEY_NAME);
    18. valB = (String) b.getString(KEY_NAME);
    19. Matcher m1 = PATTERN.matcher(valA);
    20. Matcher m2 = PATTERN.matcher(valB);
    21.  
    22. // The only way find() could fail is at the end of a string
    23.  
    24. while (m1.find() && m2.find()) {
    25.  
    26. // matcher.group(1) fetches any non-digits captured by the
    27. // first parentheses in PATTERN.
    28.  
    29. int nonDigitCompare = m1.group(1).compareTo(m2.group(1));
    30. if (0 != nonDigitCompare) {
    31. return nonDigitCompare;
    32. }
    33. // matcher.group(2) fetches any digits captured by the
    34. // second parentheses in PATTERN.
    35.  
    36. if (m1.group(2).isEmpty()) {
    37. return m2.group(2).isEmpty() ? 0 : -1;
    38. } else if (m2.group(2).isEmpty()) {
    39. return +1;
    40. }
    41. BigInteger n1 = new BigInteger(m1.group(2));
    42. BigInteger n2 = new BigInteger(m2.group(2));
    43. int numberCompare = n1.compareTo(n2);
    44. if (0 != numberCompare) {
    45. return numberCompare;
    46. }
    47. }
    48. // Handle if one string is a prefix of the other.
    49. // Nothing comes before something.
    50. return m1.hitEnd() && m2.hitEnd() ? 0 :m1.hitEnd()? -1 : +1;
    51. }
    52. });
    53. for (int i = 0; i < jsonValues.size(); i++) {
    54. sortedJsonArray.put(jsonValues.get(i));
    55. }
    56. return sortedJsonArray;
    57. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: