Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order ArrayList serializable

I have two class in JAVA:

public class Persons implements Serializable{
    String name;
    String phone;

    ...
}

and:

public class Diary implements Comparable{
    ArrayList<Persons> persons=new ArrayList();

    ...
}

I want to order my ArrayList by the name (in alphabetical), but I can't use Collections.sort() because my ArrayList is Persons class and this give me and error. I can't implements Comparable in class Persons because if i do it, i can't read later my ArrayList which is save in a Object .dat

like image 586
mjosee7 Avatar asked Feb 04 '26 02:02

mjosee7


1 Answers

try this

    Collections.sort(persons, new Comparator<Persons>() {
        @Override
        public int compare(Persons o1, Persons o2) {
            return o1.name.compareTo(o2.name);
        }});
like image 127
Evgeniy Dorofeev Avatar answered Feb 06 '26 17:02

Evgeniy Dorofeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!