5

I have arrayLists of Dates which is in String format then how can I sort this arraylists,

ArrayList<String> aryBeginDate;
ArrayList<String> aryDueDate;

for (int i = 0; i < arySubject.size(); i++) {

    RowItem row = new RowItem();
    row.setSubject(arySubject.get(i).toString());
    row.setUser(aryFromUser.get(i).toString());

    if (aryBeginDate.equals(" ")) {
        row.setStartDate(" ");
    } else {
        row.setStartDate(aryBeginDate.get(i).toString()); \\Have to sort at this line
    }

    if (aryDueDate.equals(" ")) {
        row.setEndDate(" ");
    } else {
        row.setEndDate(aryDueDate.get(i).toString()); \\Have to sort at this line
    }

    aryListBean.add(row);

}
Mit Bhatt
  • 1,605
  • 2
  • 13
  • 24

6 Answers6

12

Don't use a String when you want a Date. Use a Date. You should only transfom the Date to a String when displaying it. Otherwise, everywhere in the code, the date should of type Date. This is what allows sorting in chronological order, because dates have a natural order which is chronological.

So, once the RowItem has a startDate and an endDate, both being of type Date, you can sort a list of row items by start date using a simple comparator:

Collections.sort(rowItems, new Comparator<RowItem>() {
    @Override
    public int compare(RowItem r1, RowItem 2) {
        return r1.getStartDate().compareTo(r2.getStartDate());
    }
}); 

Also, fix your indentationof if/else blocks, because your way is really not readable:

if (aryBeginDate.equals(" ")) {
    row.setStartDate(" ");
} 
else {
    row.setStartDate(aryBeginDate.get(i).toString());
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
4

Very Simple Answer:For Ascending just call this method:

Collections.sort(mArrayList, new OutcomeAscComparator());
public class OutcomeAscComparator implements Comparator<Select>
    {
        public int compare(Select left, Select right) {
            return left.getOutcome().compareTo(right.getOutcome());
        }
    }

Select is my model, For Descending:

Collections.sort(mArrayList, new OutcomeDescComparator());
public class OutcomeDescComparator implements Comparator<Select>
    {
        public int compare(Select left, Select right) {
            return right.getOutcome().compareTo(left.getOutcome());
        }
    }

Please dont forget to refresh your list view IF you want your list to be AScending and Descending

                mListView.invalidateViews();

                mAdapter.notifyDataSetChanged();
Ahmad Arslan
  • 4,498
  • 8
  • 38
  • 59
1

This will be very painful if you try to sort this yourself with Strings. I'd recommend you to parse the String to a Date (check JodaTime for a very convenient Date/Time tool) and then sort with java.util.Collections.sort().

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • 1
    I am already feeling pain :) I have to change so much things to parse String to Date..plz Help me..I have stuck in my Project – Mit Bhatt Feb 26 '13 at 09:43
1

You can use a Comparator to sort the list. Something like this:

Collections.sort(aryListBean, new Comparator<RowItem>() {
    @Override
    public int compare(RowItem r1, RowItem r2) {
        // Place your compare logic here
    }
});
0

There is an in built function on ArrayList sort(). Since you are using String, this will work.

Aashray
  • 2,753
  • 16
  • 22
  • But it'll just sort the Strings per Alphabet, not the Dates, and I guess he wants to sort the dates. – fweigl Feb 26 '13 at 09:53
  • Numbers are also considered in the same way as strings. 1 will always come before 2 when you sort them as strings. – Aashray Feb 26 '13 at 09:56
  • can you give me an example plz?? – Mit Bhatt Feb 26 '13 at 10:39
  • First comment `Collections.sort(arr);` and run, then uncomment and run. You will see the difference:`public static void main(String[] args) { ArrayList arr=new ArrayList(); arr.add("12-12-12"); arr.add("11-12-12"); arr.add("09-12-12"); Collections.sort(arr); System.out.println(arr); }` – Aashray Feb 26 '13 at 11:41
0

First convert the String in Date and create the arraylist. Then use the Collection sort method for sorting the arraylist.

Ranu Jain
  • 577
  • 4
  • 11