How to Sort an Arraylist of Date in ascending and descending order which is in String format

后端 未结 6 1700
轻奢々
轻奢々 2020-12-17 02:36

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

ArrayList aryBeginDate;
ArrayList aryDu         


        
相关标签:
6条回答
  • 2020-12-17 03:10

    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().

    0 讨论(0)
  • 2020-12-17 03:14

    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 讨论(0)
  • 2020-12-17 03:17

    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();
    
    0 讨论(0)
  • 2020-12-17 03:18

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

    0 讨论(0)
  • 2020-12-17 03:19

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

    0 讨论(0)
  • 2020-12-17 03:21

    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());
    }
    
    0 讨论(0)
提交回复
热议问题