-2

I want to sort two json arrays stream and stream1 with respect to time together.

The record in each array with the latest time will come first.Time is formatted like value of min ago ,days ago

This is the code I have so far:

 parseJsonFeed(JSONObject response) {
            try {
                String stream = pref.getStream();
                JSONArray feedArray = response.getJSONArray(stream);


                for (int i = 0; i < feedArray.length(); i++) {
                    JSONObject feedObj = (JSONObject) feedArray.get(i);

                    FeedItem item = new FeedItem();
                    item.setId(feedObj.getInt("id"));
                    item.setName(feedObj.getString("title"));

                    // Image might be null sometimes
                    String image = feedObj.isNull("image") ? null : feedObj
                            .getString("image");
                    item.setImge(image);
                    item.setStatus(feedObj.getString("status"));
                    item.setProfilePic(feedObj.getString("profilepic"));
                    item.setTimeStamp(feedObj.getString("created_at"));

                    // url might be null sometimes
                    String feedUrl = feedObj.isNull("url") ? null : feedObj
                            .getString("url");
                    item.setUrl(feedUrl);

                    feedItems.add(item);
                }

                // notify data changes to list adapater
                listAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            String stream2 = pref.getStream2();

            if (!stream2.contentEquals("none")) {

                try {

                    JSONArray feedArray = response.getJSONArray(stream2);


                    for (int i = 0; i < feedArray.length(); i++) {
                        JSONObject feedObj = (JSONObject) feedArray.get(i);

                        FeedItem item = new FeedItem();
                        item.setId(feedObj.getInt("id"));
                        item.setName(feedObj.getString("title"));

                        // Image might be null sometimes
                        String image = feedObj.isNull("image") ? null : feedObj
                                .getString("image");
                        item.setImge(image);
                        item.setStatus(feedObj.getString("status"));
                        item.setProfilePic(feedObj.getString("profilepic"));
                        item.setTimeStamp(feedObj.getString("created_at"));

                        // url might be null sometimes
                        String feedUrl = feedObj.isNull("url") ? null : feedObj
                                .getString("url");
                        item.setUrl(feedUrl);

                        feedItems.add(item);
                    }

                    // notify data changes to list adapater
                    listAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

I need to sort the arrays together to show the most recent item in each array first, then the next most recent, and so on.

1 Answer 1

0

Make the FeedItem class implement Comparable or Comparator (refer this). Then call :

Collections.sort(feedItem);

before calling notifyDataSetChanged()

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.