java - Sorting Arraylist for a string but only by date -
i have arraylist contains "2013-7-13 \n 12 hour(s) 23 minute(s) "
such kind of records. how can sort date.
piece of code:
string date1 = c.getstring(c.getcolumnindex(tabprodb.key_date) ); string time1 = c.getstring(c.getcolumnindex(tabprodb.key_time) ); string date_time1 = date1 + "\n" +time1; toast.maketext(history.this, date_time1, toast.length_short).show(); listitems.add(date_time1);
create own comparator<string>
implementation, following:
note there no error checks, it's show how it.
public static void main(string[] args) { list<string> strings = arrays.aslist("2013-7-13 \n 12 hour(s) 23 minute(s)", "2013-7-10 \n 12 hour(s) 23 minute(s)"); collections.sort(strings, new comparator<string>() { @override public int compare(string o1, string o2) { simpledateformat format = new simpledateformat("yyyy-mm-dd"); date date1; date date2; try { date1 = format.parse(o1.split(" ")[0]); date2 = format.parse(o2.split(" ")[0]); } catch (parseexception e) { throw new illegalargumentexception("could not parse date!", e); } return date1.compareto(date2); } }); system.out.println(strings); }
Comments
Post a Comment