سؤال

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

هل كانت مفيدة؟

المحلول

try this

    Collections.sort(persons, new Comparator<Persons>() {
        @Override
        public int compare(Persons o1, Persons o2) {
            return o1.name.compareTo(o2.name);
        }});

نصائح أخرى

If you want a sorted collection of people I would do this.

class Dairy {
    final SortedMap<String, Person> people = new TreeMap<String, Person>();

}

or

class Dairy {
    final SortedSet<Person> people = new TreeSet<Person>(new MyNameComparator());

}

or

class Dairy {
    final List<Person> people = new ArrayList<Person>();

    public void sortPeople() {
        Collections.sort(people, new MyNameComparartor());
    }
}

Here is complete sample for your problem.. save below code in Test.java file and run it..

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


class Person implements Serializable{

    private static final long serialVersionUID = 1L;

    String name  ;
    String phone ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Person(String name, String phone) {
        super();
        this.name = name;
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", phone=" + phone + "]";
    }




}

class Diary {

    ArrayList<Person> list = new ArrayList<Person>();

    public ArrayList<Person> getList() {
        return list;
    }

    public void setList(ArrayList<Person> list) {
        this.list = list;
    }

    public Diary(ArrayList<Person> list) {
        super();
        this.list = list;
    }

}

public class Test
{
    public static void main(String[] args) {


        Person p1 = new Person("John","123");
        Person p2 = new Person("Aby","456");
        Person p3 = new Person("Debra","789");

        ArrayList<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);
        list.add(p3);

        Diary d = new Diary(list);

        Collections.sort(d.getList(), new Comparator<Person>(){
            public int compare(Person item1, Person item2){
                int compare = item1.getName().compareTo(item2.getName());
                return compare;
            }
        });

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

It sorts list of person in diary using comparator without touching your person class..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top