简体   繁体   中英

How to sort ArrayList of model type according to date (a string type variable in ArrayList)?

I am trying to sort an ArrayList of model type, ArrayList jobCards according to string type of date field called dueDate.

I tried adding the following in model.

@Override
public int compareTo(@NonNull TodayModel o) 
{
    return o.newDueDate.compareTo(this.newDueDate);
}

And then applying

Collections.sort(jobCards);

Though you have the right idea, your method isn't wired up to the IComparable interface.

Define your JobCards class like so.

public class JobCards : IComparable<JobCards> {

The Visual Studio editor will highlight IComparable , offering two options to correct it. Choose Implement explicitly , and let the editor add the stub method. which will be called IComparable.CompareTo ; note the difference between that and what you have. Then, replace the statement that throws a NotImplementedException with your code, and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM