I've got a list which contains x records with dates. The thing is all my dates are in the String format and come as strings from the database. I would really like to order my List by date (in String format) but I really have no clue how to do this.
Without further ado, this is my list, which is a custom list.
List<Finance> finances;
The list contains following fields:
public class Finance {
private int id;
private int categoryId;
private int limitId;
private double amount;
private String date;
private String description;
}
And this is the dateformat I have (in String): 16/10/2013 15/12/2013 15/11/2013 14/9/2013
How would I be able to sort this custom list by date? I've seen many examples with Collections.sort but I cannot use that because of my custom list type. I've also seen some examples with Comparable but I didn't really understand those..
Could anybody tell me what would be the best way to achieve a chronical order by date of mist list please? I would also like the most lightweighted method, to use as little resources as I can.
EDIT: I still didn't find a working solution (19/12) and still hope for a response here..
Thank you Yenthe
Okay since there where no good answers and barely any responses I decided to dig into it until I fixed it.
The 'easiest' way to come around this is to insert all your dates into the database as yyyy-mm-dd format. For a great explenation on that part you should look here: https://stackoverflow.com/a/5733535/2262409
When you place the date in yyyy-mm-dd and just do a order by Date in your SQLite the dates will be ordered correct. When you place them in dd-mm-yyyy they will not be ordered correct.
Long answer short: Solved it in the SQL part. I insert records in the format yyyy-mm-dd I get them by
String selectQuery = "SELECT * FROM Finance ORDER BY date desc";
And then I reformat them to dd-mm-yyyy right before the user sees it. Example:
String date = String.valueOf(values.get(position).getDate());
// we will format yyyy-mm-dd to dd-mm-yyyy for readability.
//the sql has ordered our dates correctly already.
String firstPartDate = date.substring(8, 10);
String secondPartDate = "/" + date.substring(5,7);
String thirdPartDate = "/" + date.substring(0,4);
String fullCorrectDate = firstPartDate + secondPartDate + thirdPartDate;
Log.i("firstpart", firstPartDate + secondPartDate + thirdPartDate);
dateFinance.setText(fullCorrectDate);