0

My question is that i have to display my data which is fetched from JSON file ,the json file is below. But i want the data to be in sorted form after getting from JSON file i.e after the rest call when data is obtained from controller in viewmodel,so that i can directly display it on the View Class. which has cardviews.

{
 "employeeList":[
    {
      "employeeName": "aaaa",
      "employeeStatus": "Trainee",
      "company": "IBM",
      "mobile": "894996662"
   },
   {
      "employeeName": "bbbb",
      "employeeStatus": "Fellowship",
      "company": "Wipro",
      "mobile": "9876000021"
   },
   {
      "employeeName": "cccc",
      "employeeStatus": "Fellowship",
      "company": "CGI",
      "mobile": "9876000021"
   },
   {
      "employeeName": "cccc",
      "employeeStatus": "Fellowship",
      "company": "CGI",
      "mobile": "9876000021"
   }
  ]
}

And the .java file is below,were i have not done the sorting.I need the help,Thank you.

try {
                    //As the data in rest call contained in JSONObject inside that JSONArray and inside
                    //JSONArray JSONObject is there ,Hence to get the data and set to the Model Class
                    //Creating JSON Object and obtained the data in bytes
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    Log.i("json object", "employeeList: ");
                    if (jsonObject != null) {
                        JSONArray jsonArray = jsonObject.getJSONArray("employeeList");
                        Log.i("json Array", "employeeList: " + jsonArray.length());
                        //Creating the object of modelClass

                        if (jsonArray.length() != 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject childObject = jsonArray.getJSONObject(i);
                                //Through the object of model Class the obtained data is set to the
                                //Model class
                                EnggFragModel enggFragModel = new EnggFragModel();

                                enggFragModel.setImageurl(childObject.getString("imageUrl"));
                                Log.i("image", "employeeData: "+childObject.getString("imageUrl"));
                                enggFragModel.setEmployeeName(childObject.getString("employeeName"));
                                enggFragModel.setEmployeeStatus(childObject.getString("employeeStatus"));
                                enggFragModel.setCompany(childObject.getString("company"));
                                enggFragModel.setEmployeeMobile(childObject.getString("mobile"));
                                enggFragModel.setEmployeeEmail(childObject.getString("emailId"));
                                enggFragModel.setEngineerID(childObject.getString("engineerId"));
                                Log.i("EngineerId", "employeeData: " + childObject.getString("engineerId"));
                                enggArrayList.add(enggFragModel);
                            }
                            enggViewModelInterface.enggViewMInterface(enggArrayList);
                            Log.i("Employee", "employeeList: " + enggArrayList);
                        }

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.i("Employee", "employeeList: ");
1

1 Answer 1

0

The solution is to take JSONArray "employeeList" convert it to ArrayList of JSONObject and sort it, using Collections.

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;
        }
    }
});

Here is original

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.