0

i'm trying to sort my array but from some reason i got the following error in my compareTo function:

Cannot invoke compareTo(int) on the primitive type int

my code:

private void sortFriendList()
{
    Arrays.sort(friendsList, new Comparator<FacebookUser>() {

        @Override
        public int compare(FacebookUser lhs, FacebookUser rhs) {                
            ChatMessage[] firstChatMessages = app.getDatabaseManager().getChatMessages(lhs.getId());
            ChatMessage[] lastChatMessages = app.getDatabaseManager().getChatMessages(rhs.getId());
            int firstLastMessageTime = (int) firstChatMessages[firstChatMessages.length-1].getTime();
            int lastLastMessageTime = (int) lastChatMessages[firstChatMessages.length-1].getTime();
            return firstLastMessageTime.compareTo(lastLastMessageTime);
        }
    });     
 }

the code supposed to sort an array buy time stored in long type (which i've cast to int)

4 Answers 4

3

The compareTomethod only works on objects. An primitive int is no object. You have to manually compare the int values and return either negative, zero or positive value, if this values is less, equal or greater than the other value.

Using the Integer wrapper class for that is just wasting resources.

This morning I'm more willing to provide some code:

long firstLastMessageTime = ...
long lastLastMessageTime = ...
if (firstLastMessageTime < lastLastMessageTime) {
    return -1;
}
if (firstLastMessageTime > lastLastMessageTime) {
    return 1;
}
return 0;

In some cases you could do firstLastMessageTime minus lastLastMessageTime, but that maybe lead to errors if you handle with negative values or if the difference can be bigger than a 32bit int value. So to be 100% sure, just use the above method all the time.


Additional comment:

Is the value really a int value? Usually the time is defined as the milliseconds since 1.1.1970 in Java, which as datatype is long.

Sign up to request clarification or add additional context in comments.

2 Comments

its a long, but i need to return an int doesn't i ?
Read the definition of the compareTo method you implement and it should be clear what you have to return. And yes you have to return an int value.
2

Don't cast long to int to avoid loss of precision. Rather box it to Long

long firstLastMessageTime = //...
long lastLastMessageTime = //...
return Long.valueOf(firstLastMessageTime).compareTo(Long.valueOf(lastLastMessageTime));

1 Comment

Isn't it a bit overkill to create two objects instead of two simple ifs?
2

Try something like this

        return Integer.valueof(firstLastMessageTime).compareTo(lastLastMessageTime);

Before invoking comapreTo method you have to wrap int into Integer object.

1 Comment

A little explanation of what you did may help OP (and others) even more.
0

You Cannot invoke compareTo on primitive types, You should use their wrapper classes. Integer rather than int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.