Exception while sorting a list of objects based on date property(String type)












0















I am trying to sort a list of Objects based on dates (In String). The dates are in string and the objects have date property value assigned "-" if there is no date with it.
I am doing the below code snippet to sort it however at one point it is giving me " Comparison method violates its general contract" exception. I am not able to figure out where the contract is breaking and how.



I looked into other threads but could not get much help.



Any inputs what could I be doing wrong?



Collections.sort(listOfObjects, new Comparator<ObjectType>(){

@Override
public int compare(ObjectType objectOne, ObjectType objectTwo) {
if(sortType.equalsIgnoreCase("asc")){
if(objectOne.getSomeDate().equalsIgnoreCase("-"))
return 1;
if(objectTwo.getDeliveryQueueResponseDTO().getSomeDate().equalsIgnoreCase("-"))
return -1;
sort = objectOne.getSomeDate().compareToIgnoreCase(objectTwo.getSomeDate());
}
else
sort = objectTwo.getSomeDate().compareToIgnoreCase(objectOne.getSomeDate());
}

return sort;
}









share|improve this question



























    0















    I am trying to sort a list of Objects based on dates (In String). The dates are in string and the objects have date property value assigned "-" if there is no date with it.
    I am doing the below code snippet to sort it however at one point it is giving me " Comparison method violates its general contract" exception. I am not able to figure out where the contract is breaking and how.



    I looked into other threads but could not get much help.



    Any inputs what could I be doing wrong?



    Collections.sort(listOfObjects, new Comparator<ObjectType>(){

    @Override
    public int compare(ObjectType objectOne, ObjectType objectTwo) {
    if(sortType.equalsIgnoreCase("asc")){
    if(objectOne.getSomeDate().equalsIgnoreCase("-"))
    return 1;
    if(objectTwo.getDeliveryQueueResponseDTO().getSomeDate().equalsIgnoreCase("-"))
    return -1;
    sort = objectOne.getSomeDate().compareToIgnoreCase(objectTwo.getSomeDate());
    }
    else
    sort = objectTwo.getSomeDate().compareToIgnoreCase(objectOne.getSomeDate());
    }

    return sort;
    }









    share|improve this question

























      0












      0








      0








      I am trying to sort a list of Objects based on dates (In String). The dates are in string and the objects have date property value assigned "-" if there is no date with it.
      I am doing the below code snippet to sort it however at one point it is giving me " Comparison method violates its general contract" exception. I am not able to figure out where the contract is breaking and how.



      I looked into other threads but could not get much help.



      Any inputs what could I be doing wrong?



      Collections.sort(listOfObjects, new Comparator<ObjectType>(){

      @Override
      public int compare(ObjectType objectOne, ObjectType objectTwo) {
      if(sortType.equalsIgnoreCase("asc")){
      if(objectOne.getSomeDate().equalsIgnoreCase("-"))
      return 1;
      if(objectTwo.getDeliveryQueueResponseDTO().getSomeDate().equalsIgnoreCase("-"))
      return -1;
      sort = objectOne.getSomeDate().compareToIgnoreCase(objectTwo.getSomeDate());
      }
      else
      sort = objectTwo.getSomeDate().compareToIgnoreCase(objectOne.getSomeDate());
      }

      return sort;
      }









      share|improve this question














      I am trying to sort a list of Objects based on dates (In String). The dates are in string and the objects have date property value assigned "-" if there is no date with it.
      I am doing the below code snippet to sort it however at one point it is giving me " Comparison method violates its general contract" exception. I am not able to figure out where the contract is breaking and how.



      I looked into other threads but could not get much help.



      Any inputs what could I be doing wrong?



      Collections.sort(listOfObjects, new Comparator<ObjectType>(){

      @Override
      public int compare(ObjectType objectOne, ObjectType objectTwo) {
      if(sortType.equalsIgnoreCase("asc")){
      if(objectOne.getSomeDate().equalsIgnoreCase("-"))
      return 1;
      if(objectTwo.getDeliveryQueueResponseDTO().getSomeDate().equalsIgnoreCase("-"))
      return -1;
      sort = objectOne.getSomeDate().compareToIgnoreCase(objectTwo.getSomeDate());
      }
      else
      sort = objectTwo.getSomeDate().compareToIgnoreCase(objectOne.getSomeDate());
      }

      return sort;
      }






      java collections comparator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 17:29









      user3262365user3262365

      248




      248
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You should implement correctly the comparison between to "-" (empty dates).



          If you don't you get that a="-" and b="-" you have that compare(a,b)==1 and compare(b,a)==1 so a>b and b>a which don't make sense.






          share|improve this answer
























          • I figured it out. Thanks buddy.

            – user3262365
            Nov 15 '18 at 17:56



















          1














          It basically means your comparator is not transitive. For more details, look at this question:



          "Comparison method violates its general contract!"






          share|improve this answer
























          • Can you point out in my code where it is not transitive?

            – user3262365
            Nov 15 '18 at 17:36











          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%2f53324946%2fexception-while-sorting-a-list-of-objects-based-on-date-propertystring-type%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









          1














          You should implement correctly the comparison between to "-" (empty dates).



          If you don't you get that a="-" and b="-" you have that compare(a,b)==1 and compare(b,a)==1 so a>b and b>a which don't make sense.






          share|improve this answer
























          • I figured it out. Thanks buddy.

            – user3262365
            Nov 15 '18 at 17:56
















          1














          You should implement correctly the comparison between to "-" (empty dates).



          If you don't you get that a="-" and b="-" you have that compare(a,b)==1 and compare(b,a)==1 so a>b and b>a which don't make sense.






          share|improve this answer
























          • I figured it out. Thanks buddy.

            – user3262365
            Nov 15 '18 at 17:56














          1












          1








          1







          You should implement correctly the comparison between to "-" (empty dates).



          If you don't you get that a="-" and b="-" you have that compare(a,b)==1 and compare(b,a)==1 so a>b and b>a which don't make sense.






          share|improve this answer













          You should implement correctly the comparison between to "-" (empty dates).



          If you don't you get that a="-" and b="-" you have that compare(a,b)==1 and compare(b,a)==1 so a>b and b>a which don't make sense.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 17:40









          minusminus

          1,8411113




          1,8411113













          • I figured it out. Thanks buddy.

            – user3262365
            Nov 15 '18 at 17:56



















          • I figured it out. Thanks buddy.

            – user3262365
            Nov 15 '18 at 17:56

















          I figured it out. Thanks buddy.

          – user3262365
          Nov 15 '18 at 17:56





          I figured it out. Thanks buddy.

          – user3262365
          Nov 15 '18 at 17:56













          1














          It basically means your comparator is not transitive. For more details, look at this question:



          "Comparison method violates its general contract!"






          share|improve this answer
























          • Can you point out in my code where it is not transitive?

            – user3262365
            Nov 15 '18 at 17:36
















          1














          It basically means your comparator is not transitive. For more details, look at this question:



          "Comparison method violates its general contract!"






          share|improve this answer
























          • Can you point out in my code where it is not transitive?

            – user3262365
            Nov 15 '18 at 17:36














          1












          1








          1







          It basically means your comparator is not transitive. For more details, look at this question:



          "Comparison method violates its general contract!"






          share|improve this answer













          It basically means your comparator is not transitive. For more details, look at this question:



          "Comparison method violates its general contract!"







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 17:35









          mjuarezmjuarez

          10.2k73954




          10.2k73954













          • Can you point out in my code where it is not transitive?

            – user3262365
            Nov 15 '18 at 17:36



















          • Can you point out in my code where it is not transitive?

            – user3262365
            Nov 15 '18 at 17:36

















          Can you point out in my code where it is not transitive?

          – user3262365
          Nov 15 '18 at 17:36





          Can you point out in my code where it is not transitive?

          – user3262365
          Nov 15 '18 at 17:36


















          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%2f53324946%2fexception-while-sorting-a-list-of-objects-based-on-date-propertystring-type%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

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues