Exception while sorting a list of objects based on date property(String type)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
add a comment |
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
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
java collections comparator
asked Nov 15 '18 at 17:29
user3262365user3262365
298
298
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
I figured it out. Thanks buddy.
– user3262365
Nov 15 '18 at 17:56
add a comment |
It basically means your comparator is not transitive. For more details, look at this question:
"Comparison method violates its general contract!"
Can you point out in my code where it is not transitive?
– user3262365
Nov 15 '18 at 17:36
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I figured it out. Thanks buddy.
– user3262365
Nov 15 '18 at 17:56
add a comment |
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.
I figured it out. Thanks buddy.
– user3262365
Nov 15 '18 at 17:56
add a comment |
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.
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.
answered Nov 15 '18 at 17:40
minusminus
1,8511113
1,8511113
I figured it out. Thanks buddy.
– user3262365
Nov 15 '18 at 17:56
add a comment |
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
add a comment |
It basically means your comparator is not transitive. For more details, look at this question:
"Comparison method violates its general contract!"
Can you point out in my code where it is not transitive?
– user3262365
Nov 15 '18 at 17:36
add a comment |
It basically means your comparator is not transitive. For more details, look at this question:
"Comparison method violates its general contract!"
Can you point out in my code where it is not transitive?
– user3262365
Nov 15 '18 at 17:36
add a comment |
It basically means your comparator is not transitive. For more details, look at this question:
"Comparison method violates its general contract!"
It basically means your comparator is not transitive. For more details, look at this question:
"Comparison method violates its general contract!"
answered Nov 15 '18 at 17:35
mjuarezmjuarez
10.6k73954
10.6k73954
Can you point out in my code where it is not transitive?
– user3262365
Nov 15 '18 at 17:36
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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