为众多领域实施排序比较器

时间:2014-11-04 23:49:21

标签: java arraylist

我有一个包含15个字段的Java对象(需要比较5个字段),这个对象包含在arraylist中。 arraylist需要排序,所以我需要实现比较器接口......

如何实现比较器接口以逐个比较一个字段...

示例:

Class Person(){
        private String fname;
        private String mname;
        private String lname;
        private String age;
        private String address;

        get/set methods;

        public static Comparator<Person> per = new Comparator<Person>{

        String x1 = ((Person) o1).getfame();
        String x2 = ((Person) o2).getfame();
        return x1 - x2;
        }

}

我能够为一个字段fname实现日志,但是如何实现所有剩余的4个字段(总共5个字段)。

3 个答案:

答案 0 :(得分:2)

那样的话:

public int compareTo(Person other) {
    int comp = fname.compareTo(other.fname); //Try fname first.
    if(comp != 0) return comp; //But if the fnames are equal, try the next field.
    comp = lname.compareTo(other.lname); //And so on...
    if(comp != 0) return comp;
    comp = mname.compareTo(other.mname);
    if(comp != 0) return comp;
    comp = age.compareTo(other.age);
    if(comp != 0) return comp;
    return address.compareTo(other.address); //At the last field, you can return 0 even if they are equal.
}

确保Person类实现Comparable<Person>,您应该能够在列表中对它们进行排序。

编辑:对您的问题感兴趣,我一直在阅读有关排序字符串的内容,事实证明上述代码可以正常工作,但会区分大小写。这可能足以满足您的需要,但意味着"Z"将在"a"之前排序,因为"Z"是大写字母,而那些获得的Unicode索引较低。

显然,String.CASE_INSENSITIVE_ORDER中有一个预先构建的比较器,可以比较字符串,使"Z"正确地放在"a"后面。如果要使用它,请使用int comp = String.CASE_INSENSITIVE_ORDER.compare(fname,other.fname);(并对每个字段重复)。还有一个班级java.text.Collator,可以正确地对除英语之外的其他地方进行排序。我今天学到了新东西!

答案 1 :(得分:0)

试试这个:

class Person implements Comparable<Person>{

    private String fname;
    private String mname;
    private String lname;
    private String age;
    private String address;
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getMname() {
        return mname;
    }
    public void setMname(String mname) {
        this.mname = mname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public int compareTo(Person o2) {

        return  
        (fname+lname+mname+age+address).compareTo(o2.fname+o2.lname+o2.mname+o2.age+o2.address);
    }

}

答案 2 :(得分:0)

/*You can try this*/
    package test1;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;

    public class PersonComparisonTest {

        public static void main(String[] args) {

            Person p1 = new Person("A","B","C","L","28");
            Person p2 = new Person("X","Y","Z","N","29");
            Person p3 = new Person("L","M","N","M","30");
            ArrayList persons = new ArrayList();
            persons.add(p1);
            persons.add(p2);
            persons.add(p3);
            printDATA(persons.toArray());
            Collections.sort(persons, new PersonComparator());
            printDATA(persons.toArray());
        }
        public static void printDATA(Object[] als) {  
            for(int i=0; i < als.length; i++) {  
                System.out.println(als[i]);  
            }  
        }
    }
    class PersonComparator implements Comparator<Person>{

        public int compare(Person p1, Person p2){
            int i = p1.getFname().compareTo(p2.getFname());
            if (i != 0) return i;

            i = p2.getMname().compareTo(p2.getMname());
            if (i != 0) return i;

            i = p2.getLname().compareTo(p2.getLname());
            if (i != 0) return i;

            i = p2.getAddress().compareTo(p2.getAddress());
            if (i != 0) return i;

            return Integer.valueOf(p1.getAge()).compareTo(Integer.valueOf(p2.getAge()));
        }

    }

    class Person{
        private String fname;
        private String mname;
        private String lname;
        private String age;
        private String address;
        public Person(String fname, String mname, String lname, String address,String age) {
            super();
            this.fname = fname;
            this.mname = mname;
            this.lname = lname;
            this.address = address;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person [fname=" + fname + ", mname=" + mname + ", lname="
                    + lname + ", age=" + age + ", address=" + address + "]";
        }
        public String getFname() {
            return fname;
        }
        public void setFname(String fname) {
            this.fname = fname;
        }
        public String getMname() {
            return mname;
        }
        public void setMname(String mname) {
            this.mname = mname;
        }
        public String getLname() {
            return lname;
        }
        public void setLname(String lname) {
            this.lname = lname;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }