0

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?

Anna
  • 499
  • 1
  • 6
  • 24
  • you looking for how to sort by the name or how to combine different properties for your sorting ? – Emerson Cod Feb 08 '16 at 10:48
  • try java's comparator interface and check whether you can achieve what you want. Tutorial http://www.javatpoint.com/Comparator-interface-in-collection-framework – Ankit Aggarwal Feb 08 '16 at 10:50

3 Answers3

1

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.

Community
  • 1
  • 1
Xerillio
  • 4,855
  • 1
  • 17
  • 28
0
    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]
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
0
public static Comparator<? super Model2> sorting() {
        return new Comparator<Model2>() {
           final Comparator<Model2> idComparator = Comparator.comparing(Model2::getId);
           final Comparator<Model2> nameComparator = Comparator.comparing(Model2::getName);
            @Override
            public int compare(Model2 o1, Model2 o2) {
                var idCompResult = idComparator.compare(o1, o2);
                if(idCompResult != 0){
                   return idCompResult;
                }
                return nameComparator.compare(o1,o2);
            }
        };
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 08:13