-1

I have a custom object from which I get date and time in the form of a string. Its in a for loop.

List<MyObj> objs = getActivity().getPreferences().getData(activity);
for (MyObj myob : objs) {

int pos = adapter.contains(myob.id);
 if (pos == -1) {
          data = new MyAdapter.MyData();
          data.myob = myob;
          adapter.addItem(data);

         }
         else {
           data = adapter.getItem(pos);
              data.myob = myob;
         }

//Sorting to be done here... I get date from data.myob.date which returns String as 22.05 12:45
//After sorting, I need to notify the adapter.

adapter.notifyDataSetChanged();

}

Now I need to sort out the dates from the custom object in ascending order and descending order both. How can I do that?

Del905241
  • 25
  • 3

1 Answers1

1

Try this

Collections.sort(yourList, new Comparator<CustomClass>() {
                        @Override
                        public int compare(CustomObject o1, CustomObject o2) {

                            DateFormat date = new SimpleDateFormat("dd MMM yyyy");
                            Date d1 = null, d2 = null;
                            try {
                                d1 = date.parse(o1.get_date().toString());
                                d2 = date.parse(o2.get_date().toString());

                            } catch (ParseException e) {
                                e.printStackTrace();
                            }

                            //return o1.get_date().compareTo(o2.get_date());
                            return d1.compareTo(d2);
                        }
                    });
AbhayBohra
  • 2,047
  • 24
  • 36