1

I've read all answers etc, but non of them solved my problem.

I have basic function to sort list of custom object array by String InvoiceNumber.

List<Invoice> invoiceList = new ArrayList<>();

...
// invoiceList filled here
...

Collections.sort(invoiceList, new Comparator<Invoice>()
{
    @Override
    public int compare(Invoice o1, Invoice o2)
    {
           return o1.InvoiceNumber.compareTo(o2.InvoiceNumber);
    }
});

Some part of Invoice class.

public class Invoice extends AttributeContainer
implements KvmSerializable,android.os.Parcelable, Comparable
{

public String CustomerId;

public String InvoiceNumber;

Picture of more code. Yea.

However, it never calls compare method.

I'm using android studio with latest libraries. Is there anything that I'm missing?

17
  • 7
    How are you sure it never calls it? Commented Apr 25, 2017 at 19:57
  • 3
    Post the class / method which utilizes this comparator. We cannot determine why it's not being called (or if it's not even being called for that matter) with the little snippet of code you've posted. Commented Apr 25, 2017 at 20:01
  • 3
    Are you sure invoiceList has at least 2 elements? Commented Apr 25, 2017 at 20:01
  • 1
    It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it? Commented Apr 25, 2017 at 20:02
  • 1
    Note that Invoice shouldn't implement Comparable, but rather Comparable<Invoice>. And you don't really need to implement Comparable<Invoice> (or Comparable) if you're providing a Comparator; but you can, if you so desire. Commented Apr 25, 2017 at 20:13

2 Answers 2

7

However, it never calls compare method.

The only time that the compare(...) method would not be called is if your list has 0 or 1 items in it. Probably this is a debugging problem with Android and the compare(...) is being called but maybe the breakpoint is being skipped. Putting some sort of log messages inside of the method will confirm this.

I suspect that your problem is here:

public String InvoiceNumber;

First of all the field name should be invoiceNumber with a starting lowercase letter. Also it probably should not be public for data hiding reasons.

But I suspect that real problem is with String comparisons of numbers. For example if you have the following invoice-number strings in your entities:

1
2
3
10
11
20
25
114
201

Then these will sort like:

1
10
11
114
2
20
25
201
3

That's how the String.compare(...) method would sort it because the string "3" is greater than the string "201". If you want them to be sorted as numbers then you should make the invoiceNumber field be an int or long.

Hope this helps.

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

Comments

0

I had the same problem when creating anonymous class for the sort method. Try to extract your Comparator class and you'll be able to debug.

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.