F
F
Fotonick2016-08-19 16:43:49
Android
Fotonick, 2016-08-19 16:43:49

Why doesn't Collections.sort sort the same way?

In my application there are 4 tabs with data in lists. To sort the data by the time of adding, I use the ordinal numbers of the id of the objects. In three tabs, the data is sorted correctly, but not in the last one.
So here is the code

Collections.sort(allMentorsList, new Comparator<OneMentorClass>() {
                        @Override
                        public int compare(OneMentorClass o1, OneMentorClass o2) {
                            return o2.getMentorID().compareTo(o1.getMentorID());
                        }
                    });

works correctly and sorts the entire list of data, while the identical
Collections.sort(allLawyersList, new Comparator<OneLawyerClass>() {
                        @Override
                        public int compare(OneLawyerClass o1, OneLawyerClass o2) {
                            return o2.getLawyerID().compareTo(o1.getLawyerID());
                        }
                    });

no, and sorts the objects as if in pairs, that is, in the first pair, the id of the first object is greater than the second, and in the second pair, the id of the first object is greater than the second, but in the second pair there may be an object with an id greater than the object in the first pair. How can this be?
All objects request identical data in identical variable formats using identical methods.
Maybe a compilation glitch? I'm using the latest Android Studio with canary channel and gradle in beta.
PS
The classes are similar, differing only in the name of the variables and their number (I removed the extra variables in the example)
, that is, the objects of this class are sorted normally
public class OneMentorClass {
    private String mentorID;

    public OneMentorClass(String mentorID) {
        this.mentorID = mentorID;
    }

    public String getMentorID() {
        return mentorID;
    }
}

but the objects are not
public class OneLawyerClass {
    private String lawyerID;

    public OneLawyerClass(String lawyerID) {
        this.lawyerID = lawyerID;
    }

    public String getLawyerID() {
        return lawyerID;
    }

SORTING EXAMPLE Collections.sort
correct sorting
takes as input allMentorsList with objects in this order id
mentorID=19
mentorID=20
mentorID=21
mentorID=23
mentorID=24
mentorID=31
Gives as output allMentorsList with objects in descending order id
mentorID=31
mentorID= 24
mentorID=23
mentorID=21
mentorID=20
mentorID=19
Wrong sorting
takes as input allLawyersList with objects in this order id
lawyerID=6
lawyerID=8
lawyerID=18
lawyerID=21
Gives as input allLawyersList with objects in this order id
lawyerID=8
lawyerID=6
lawyerID=21
lawyerID=18

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-08-19
@Photonick

Your ID is String. He compares them in lexicographical order because he doesn't know they are numbers. Therefore 25 is greater than 100, for example (because 2 is greater than 1). You look - you got IDs in descending order of the first digit. You need to parse them as numbers or write your own comparator, which will compare correctly.
In addition, when you have Comparable elements to sort, you don’t need to write your own comparator for the reverse order at all. There is Collections.reverseOrder().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question