-1

i am developing a news app which it composed of:

Title,description,time,date

The date format is as : 12/7/2017

When i am adding multiples news they are showed randomly on the recyclerview.

What is the best way to use Comparator object to sort the arraylist by date so when i add a news it must show up in recyclerview top.

This is my working code :

 JSONArray array = new JSONArray(response);
            JSONObject jsonObject = null;
            post_array2.clear();
            Simplenews_data p;
            for (int i = 0; i < array.length(); i++) {
                jsonObject = array.getJSONObject(i);

                int id_simplenews = jsonObject.getInt("id_simplenews");
                String name_simplenews = jsonObject.getString("name_simplenews");
                String image_simplenews = jsonObject.getString("image_simplenews");
                String desc_simplenews = jsonObject.getString("desc_simplenews");
                String time_simplenews = jsonObject.getString("time_simplenews");
                String date_simplenews = jsonObject.getString("date_simplenews");



                p = new Simplenews_data();
                p.setId_simplenews(id_simplenews);
                p.setName_simplenews(name_simplenews);
                p.setImage_simplenews(image_simplenews);
                p.setDesc_simplenews(desc_simplenews);
                p.setTime_simplenews(time_simplenews);
                p.setDate_simplenews(date_simplenews);

                post_array2.add(p);

I have searched and found this code that works for other issue if u have to compare two integers :

Collections.sort(post_array, new Comparator<Standings_data>(){
                public int compare(Standings_data s1, Standings_data s2) {
                    return s1.getPts().compareToIgnoreCase(s2.getPts());
                }
            });

But really i don 't have any idea how to sort it by this date format so when a news come it shows in top of recyclerview not randomly.

This a simple screenshot of the current situation:

enter image description here

Mahdi H
  • 339
  • 8
  • 24
  • 2
    Create a Date object out of the string, using a SimpleDateFormat.parse() and then use date's compareTo method. – Vucko Jul 12 '17 at 14:10
  • Would you please post some code – Mahdi H Jul 12 '17 at 14:17
  • I would not. You could just google for what I said and find the answer easily. But you'd rather for someone to do everything for you. For that, there are sites as well, but you need to pay usually. Cheers. – Vucko Jul 12 '17 at 14:20
  • Thanks i found the solution but no need to downvote my question .. – Mahdi H Jul 12 '17 at 14:34

2 Answers2

1

Extract year, month and day by spliting your String :

String[] parts = date.split("/");

Convert into Integers then compare year, month and day.

Should works.

(Vucko answer is better if you can parse with SimpleDateFormat)

Sigma
  • 532
  • 3
  • 8
  • If i did that how can i make the recyclerview shows the items by the compared date ? – Mahdi H Jul 12 '17 at 14:19
  • Well, the RecyclerView display the items in the same order than the ArrayList. So sort the ArrayList before displaying it or call the `notifyDataSetChanged` function once the ArrayList sorted. – Sigma Jul 12 '17 at 14:21
1

You could convert the Strings to Dates like Vucko recommends, or you could convert the dates to ISO 8601 format and sort them alphabetically.

ISO 8601 looks like YYYY-mm-dd so alphabetically sorting these as strings will sort by year, month, then day, which effectively sorts the strings chronologically.

isodate = date.subString(5) + date.subString(0,2) + date.substring(3,4) This will leave you with an edge case of dates with 1 or 2 numbers (7 vs 17) but that should hopefully encourage you to use a better date format. You can find out how to sort the ArrayList here Sorting a collection of objects

MacLean Sochor
  • 435
  • 5
  • 14