Java Collections.sort doesn't call compare method












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?










share|improve this question




















  • 7





    How are you sure it never calls it?

    – maszter
    Apr 25 '17 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.

    – Jake Miller
    Apr 25 '17 at 20:01






  • 3





    Are you sure invoiceList has at least 2 elements?

    – Andy Turner
    Apr 25 '17 at 20:01






  • 1





    It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

    – Lew Bloch
    Apr 25 '17 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.

    – Andy Turner
    Apr 25 '17 at 20:13


















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?










share|improve this question




















  • 7





    How are you sure it never calls it?

    – maszter
    Apr 25 '17 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.

    – Jake Miller
    Apr 25 '17 at 20:01






  • 3





    Are you sure invoiceList has at least 2 elements?

    – Andy Turner
    Apr 25 '17 at 20:01






  • 1





    It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

    – Lew Bloch
    Apr 25 '17 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.

    – Andy Turner
    Apr 25 '17 at 20:13
















1












1








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?










share|improve this question
















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?







java sorting collections






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 25 '17 at 21:38







MmtBkn

















asked Apr 25 '17 at 19:50









MmtBknMmtBkn

622925




622925








  • 7





    How are you sure it never calls it?

    – maszter
    Apr 25 '17 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.

    – Jake Miller
    Apr 25 '17 at 20:01






  • 3





    Are you sure invoiceList has at least 2 elements?

    – Andy Turner
    Apr 25 '17 at 20:01






  • 1





    It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

    – Lew Bloch
    Apr 25 '17 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.

    – Andy Turner
    Apr 25 '17 at 20:13
















  • 7





    How are you sure it never calls it?

    – maszter
    Apr 25 '17 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.

    – Jake Miller
    Apr 25 '17 at 20:01






  • 3





    Are you sure invoiceList has at least 2 elements?

    – Andy Turner
    Apr 25 '17 at 20:01






  • 1





    It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

    – Lew Bloch
    Apr 25 '17 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.

    – Andy Turner
    Apr 25 '17 at 20:13










7




7





How are you sure it never calls it?

– maszter
Apr 25 '17 at 19:57





How are you sure it never calls it?

– maszter
Apr 25 '17 at 19:57




3




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.

– Jake Miller
Apr 25 '17 at 20:01





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.

– Jake Miller
Apr 25 '17 at 20:01




3




3





Are you sure invoiceList has at least 2 elements?

– Andy Turner
Apr 25 '17 at 20:01





Are you sure invoiceList has at least 2 elements?

– Andy Turner
Apr 25 '17 at 20:01




1




1





It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

– Lew Bloch
Apr 25 '17 at 20:02





It's confusing to see InvoiceNumber spelled like a type name but used like a member variable. Which is it?

– Lew Bloch
Apr 25 '17 at 20:02




1




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.

– Andy Turner
Apr 25 '17 at 20:13







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.

– Andy Turner
Apr 25 '17 at 20:13














2 Answers
2






active

oldest

votes


















6















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.






share|improve this answer
























  • Ohhh.. You're right!! There is debugging problem.

    – MmtBkn
    Apr 25 '17 at 22:12



















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43619726%2fjava-collections-sort-doesnt-call-compare-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6















    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.






    share|improve this answer
























    • Ohhh.. You're right!! There is debugging problem.

      – MmtBkn
      Apr 25 '17 at 22:12
















    6















    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.






    share|improve this answer
























    • Ohhh.. You're right!! There is debugging problem.

      – MmtBkn
      Apr 25 '17 at 22:12














    6












    6








    6








    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.






    share|improve this answer














    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 25 '17 at 22:08









    GrayGray

    96.5k14230300




    96.5k14230300













    • Ohhh.. You're right!! There is debugging problem.

      – MmtBkn
      Apr 25 '17 at 22:12



















    • Ohhh.. You're right!! There is debugging problem.

      – MmtBkn
      Apr 25 '17 at 22:12

















    Ohhh.. You're right!! There is debugging problem.

    – MmtBkn
    Apr 25 '17 at 22:12





    Ohhh.. You're right!! There is debugging problem.

    – MmtBkn
    Apr 25 '17 at 22:12













    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.






    share|improve this answer




























      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.






      share|improve this answer


























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 10:11









        htafoyahtafoya

        9,81194563




        9,81194563






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43619726%2fjava-collections-sort-doesnt-call-compare-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen