I have Java bean class and I want to sort list of these beans by one bean attribute that is of String type. How can I do that?
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.
Either make the type itself implement
Comparable<Foo>, implementing thecompareTomethod by comparing the two strings, or implement aComparator<Foo>which again compares by the strings.With the first approach, you’d then just be able to use
Collections.sort()directly; with the second you’d useCollections.sort(collection, new FooComparator());For example: