Anna February 2016

How to sort list with multiple condition?

I have ArrayList with name and id.

list.add(new Model2(null, "name1"));
list.add(new Model2("13", "name2"));
list.add(new Model2(null, "name1"));
list.add(new Model2("13", "name4"));
list.add(new Model2(null, "name1"));
list.add(new Model2("13", "name6"));

First if id is null and then by the name

//the result should by like this:
    //1. null , name1
    //2. null , name1
    //3. null , name1
    //4. 13,name2
    //5. 13,name4
    //6. 13,name6

I know how to sort the null values:

Collections.sort(list,sorting());   

    public static Comparator<? super Model2> sorting() {
        Comparator<Model2> sortedList = new Comparator<Model2>() {
            @Override
            public int compare(Model2 o1, Model2 o2) {
                return o1.getId() == null ? -1 : 0;
            }
        };
        return sortedList;
    }

But what about the names values?

Answers


Xerillio February 2016

First you have to figure out how you want to compare two strings. Should name2 come before name10, then you need to extract the number from the string. See here how to do that.

You can order strings in many different ways, so I'll just describe the idea. In the compare method, you should have something like the following instead:

public int compare(Model2 o1, Model2 o2) {
    if (o1.getId() == null) return -1;
    else if (o2.getId() == null) return 1;
    else return compareStrings(o1.getName(), o2.getName());
}

The thing you have to consider here is how to implement the compareStrings method.

P.S. You can of course write it into a single return statement, but I thought if-statements would be more readable.


Essex Boy February 2016

    List<Model2> list = new ArrayList<Model2>();

    list.add(new Model2(null, "name1"));
    list.add(new Model2("13", "name2"));
    list.add(new Model2(null, "name3"));
    list.add(new Model2(null, "name2"));
    list.add(new Model2("13", "name6"));
    list.add(new Model2("14", "name7"));
    list.add(new Model2("13", "name4"));
    list.add(new Model2("14", "name3"));

    Collections.sort(list, new Comparator<Model2>() {

        @Override
        public int compare(Model2 o1, Model2 o2) {
            if (o1.getId() != null && o2.getId() != null) {
                if (o1.getId().equals(o2.getId())) {
                    return o1.getName().compareTo(o2.getName());
                }
                return o1.getId().compareTo(o2.getId());
            } else if (o1.getId() == null) {
                if (o2.getId() == null) {
                    return o1.getName().compareTo(o2.getName());
                }
                return -1;
            } else if (o2.getId() == null) {
                if (o1.getId() == null) {
                    return o1.getName().compareTo(o2.getName());
                }
            }
            return 0;
        }
    });

    for (Model2 model2 : list) {
        System.out.println(model2);
    }
}

Gives

Model2 [id=null, name=name1]
Model2 [id=null, name=name2]
Model2 [id=null, name=name3]
Model2 [id=13, name=name2]
Model2 [id=13, name=name4]
Model2 [id=13, name=name6]
Model2 [id=14, name=name3]
Model2 [id=14, name=name7]

Post Status

Asked in February 2016
Viewed 3,704 times
Voted 6
Answered 2 times

Search




Leave an answer