Sunday, 15 April 2012

android - SortedSet (TreeSet) in descending order -



android - SortedSet<Date> (TreeSet<Date>) in descending order -

i want sort sortedset<date> in descending order.

i have tried collection sorting , other options none works.

i've been googling lot didn't solution. code below:

hashmap<date, arraylist<hashmap<string, string>>> tldata = new hashmap<date, arraylist<hashmap<string, string>>>(); sortedset<date> keys; public void processdata(string result) { calendar cal = calendar.getinstance(); seek { jsonarray arr = new jsonarray(result); int len = arr.length(); arraylist<hashmap<string, string>> tmplist; hashmap<string, string> map; tldata = new hashmap<date, arraylist<hashmap<string, string>>>(); (int = 0; < len; ++i) { jsonobject obj = arr.getjsonobject(i); string datestr = obj.getstring("timestamp"); date d = df.parse(datestr); cal.settime(d); cal.set(cal.get(calendar.year), cal.get(calendar.month), cal.get(calendar.date), 0, 0, 0); d = cal.gettime(); tmplist = tldata.get(d); if (tmplist == null) tmplist = new arraylist<hashmap<string, string>>(); map = new hashmap<string, string>(); string val = stringescapeutils.unescapexml(stringescapeutils .unescapehtml(obj.getstring("ti"))); map.put("val", val); map.put("id", obj.getstring("id")); tmplist.add(map); tldata.put(d, tmplist); } } grab (jsonexception e) { e.printstacktrace(); } grab (parseexception e) { e.printstacktrace(); } keys = new treeset<date>(tldata.keyset()); // want sort set descending order. }

instanciate treeset reverse comparator , elements:

private static final comparator<date> rev_date_comp = new comparator<date>() { @override public int compare(date d1, date d2) { homecoming d2.compareto(d1); } } // ... keys = new treeset<date>(rev_date_comp); // init reverse comparator keys.addall(tldata.keyset());

android sorting collections

No comments:

Post a Comment