I need to sort my ArrayList according to date which is in Student object… If the date(joining date) is same then i need to sort it according to ID… how to do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This can be done in two common ways.
The easy way
1) Having the Student provide an implementation of Comparable, which ultimately will defer to the Date. This is a very simple operation, but it is a bit of a hack.
The maintainable way
2) Alternatively,the Collections.sort method takes a Comparator as input – and you can easily write a custom comparator that implements the comparison necessary for sorting, by casting the inputs into Student classes, and directly returning the data comparison.
The second solution is more modular and maintainable, unless comparison by date is s fundamental , central aspect of your application.