Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort JSON Array from JSON Objects in Android?

I have a JSON array, and I want to sort it Depending on the name and too alphabetically. My Json is as follows.

[
    {
        "UserID": 77,
        "InvID": 333,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Summet",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 334,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Amit",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 3,
        "InvID": 335,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Amitesh",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 336,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "FOO",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 337,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Krazy",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 1,
        "InvID": 338,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Test",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 339,
        "Email": "[email protected]",
        "Phone": "",
        "Name": " ",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 340,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "SAM",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 101,
        "InvID": 343,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Anurag",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 95,
        "InvID": 379,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "S",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 428,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Summet",
        "Company": "",
        "Date": "2014-01-16T00:00:00"
    },
    {
        "UserID": 4,
        "InvID": 494,
        "Email": "[email protected]",
        "Phone": "",
        "Name": "Sameer P",
        "Company": "",
        "Date": "2014-01-21T00:00:00"
    },
    {
        "UserID": 85,
        "InvID": 507,
        "Email": "[email protected]",
        "Phone": "9404988188",
        "Name": "Jonny Dep",
        "Company": "Iconnect",
        "Date": "2014-01-22T00:00:00"
    }
]

As you can see I have a key value "Name" and in that I get respected names. I want to sort this Json array completely based on this names and that too alphabetically.Thanks in advance.

like image 386
user2699728 Avatar asked Dec 26 '22 14:12

user2699728


2 Answers

Convert JSONArray to Arraylist<JSONBbject> and use Collections to sort your arraylist

for example :

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    try {
        array.add(jsonArray.getJSONObject(i));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Collections.sort(array, new Comparator<JSONObject>() {

    @Override
    public int compare(JSONObject lhs, JSONObject rhs) {
        // TODO Auto-generated method stub

        try {
            return (lhs.getString("Name").toLowerCase().compareTo(rhs.getString("Name").toLowerCase()));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
});
like image 110
henry4343 Avatar answered Dec 28 '22 05:12

henry4343


Firstly, parse the json data and add every json object to List of Object (e.g. Employee)

List<Employee> employees = parseJson(jsonDate);
Collections.sort(employees, new EmployeeComparator());

Here is the comparator implementation:

class EmployeeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
like image 23
sudanix Avatar answered Dec 28 '22 04:12

sudanix