Reputation: 1924
The JSONArray below should be re-arranged according to key "date_course". For example, For the year 2015,
January
--
February
--
March
--
April
--
..etc.
[
{
"id": "1",
"date_course": "2015-01-29",
"nom": "Wow Urbain de St Pierre",
"lieu": "St Pierre",
"zone": "Nord",
"distance": "15",
"denivele": "5000m cumule",
"lieu_depart": "St-Pierre",
"lieu_arrivee": "Fin de chantier",
"tarifs": "15",
"dossards": "Ou recuperer les dossards",
"challenge": "Challenge Decathlon",
"parcours": "information",
"image_parcours": "link to image",
"organisateur_id": "1",
"nom_organisateur": "Organisateur 1",
"email_organisateur": "[email protected]",
"telephone_organisateur": "1234343",
"site_web_organisateur": "site.com",
"site_inscription_organisateur": null,
"course_qualificative": "0",
"categorie_id": "1"
},
{
"id": "2",
"date_course": "2015-01-09",
"nom": "Sentier de L'onf: La Providenc",
"lieu": "Saint Denis",
"zone": "Nord",
"distance": "11",
"denivele": "800m cumule",
"lieu_depart": "Allee de la foret 97400 Saint Denis",
"lieu_arrivee": "Saint Denis",
"tarifs": "18",
"dossards": "Ou recuperer les dossards",
"challenge": "Challenge Wow",
"parcours": "information",
"image_parcours": "link to image",
"organisateur_id": "2",
"nom_organisateur": "Organisateur 2",
"email_organisateur": "[email protected]",
"telephone_organisateur": null,
"site_web_organisateur": null,
"site_inscription_organisateur": null,
"course_qualificative": "1",
"categorie_id": "2"
},
{
"id": "3",
"date_course": "2014-12-17",
"nom": "Wow Urbain de St Paul",
"lieu": "St Paul",
"zone": "Est",
"distance": "19",
"denivele": "500m cumule",
"lieu_depart": "St-Paul",
"lieu_arrivee": "St-Paul",
"tarifs": "10",
"dossards": "Ou recuperer les dossards",
"challenge": "Challenge City Sport",
"parcours": "information",
"image_parcours": "link to image",
"organisateur_id": "2",
"nom_organisateur": "Organisateur 3",
"email_organisateur": "[email protected]",
"telephone_organisateur": "35645654",
"site_web_organisateur": "organisateur.com",
"site_inscription_organisateur": "siteinscription.com",
"course_qualificative": "0",
"categorie_id": "3"
},
{
"id": "4",
"date_course": "2015-03-11",
"nom": "Sentier de la Marie",
"lieu": "St Gilles",
"zone": "Ouest",
"distance": "14",
"denivele": "800m cumule",
"lieu_depart": "Mairie de St Gilles",
"lieu_arrivee": "Mairie de St Gilles",
"tarifs": "18",
"dossards": "Ou recuperer les dossards",
"challenge": "Challenge Wow",
"parcours": "information",
"image_parcours": "link to image",
"organisateur_id": "2",
"nom_organisateur": "Organisateur 4",
"email_organisateur": null,
"telephone_organisateur": null,
"site_web_organisateur": "organisateur4.com",
"site_inscription_organisateur": "organisateur4.com",
"course_qualificative": "0",
"categorie_id": "1"
}
]
I extracted the year and month for every date_course tag but now i am stuck in matching the dates and rearanging the arraylist. Here is my workings.
ArrayList<CourseCopy>filteredList = new ArrayList<CourseCopy>();
Date date = null;
if (myApp.getArrCalendarModelList()!=null){
calItem = myApp.getArrCalendarModelList();
for (int i=0;i<calItem.size();i++){
String string = calItem.get(i).date_course;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
date = format.parse(string);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
filteredList.add(new CourseCopy(date,calItem.get(i).nom,calItem.get(i).lieu,calItem.get(i).zone,calItem.get(i).distance,calItem.get(i).denivele,
calItem.get(i).lieu_depart,calItem.get(i).lieu_arrivee,calItem.get(i).tarifs,calItem.get(i).dossards,calItem.get(i).challenge,
calItem.get(i).parcours,calItem.get(i).image_parcours,calItem.get(i).organisateur_id,calItem.get(i).nom_organisateur,calItem.get(i).email_organisateur,
calItem.get(i).telephone_organisateur,calItem.get(i).site_web_organisateur,calItem.get(i).site_inscription_organisateur,calItem.get(i).course_qualificative,calItem.get(i).categorie_id));
}
Collections.sort(filteredList, new Comparator<CourseCopy>() {
@Override
public int compare(CourseCopy lhs, CourseCopy rhs) {
// TODO Auto-generated method stub
return lhs.getDate_course().compareTo(rhs.getDate_course());
}
});
}
Can someone give me a hint or help me to get the desired results please.
Upvotes: 1
Views: 530
Reputation: 2922
just convert String date to Date object
for (int i=0;i<calItem.size();i++){
String string = calItem.get(i).date_course;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
calItem.get(i).setDate(date);
}
store date in each Item in your calItem
after that you can sorting it just like this
Collections.sort(calItem, new Comparator<Item>() {
public int compare(Item o1, Item o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
update:
you can call it like this
if (myApp.getArrCalendarModelList()!=null){
calItem = myApp.getArrCalendarModelList();
for (int i=0;i<calItem.size();i++){
String string = calItem.get(i).date_course;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
calItem.get(i).setDate(date);
}
Collections.sort(calItem, new Comparator<Item>() {
public int compare(Item o1, Item o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
}
Upvotes: 1