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: