Different output calling javascript sort function twice












2















I'm facing with a sorting problem in javascript. I wrote a simple sorting function where I want the null elements on top:



This is the function:



var mySort = function(a, b) {
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return -1;
if (a > b) return 1;
if (a < b) return -1;
return 0;
}


Let's take the following array as example:



var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];


By calling array.sort(mySort) the non null values are always sorted, but the null values position alternate from the beginning and the end of the array:



Odd calls: [null, null, 1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9]
Even calls: [1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9, null, null]


Why?



Edit:

In a comment HMR head me to an error in the algorithm: if (!b) return -1; should be if (!b) return 1;. Now it works fine with stings and numbers.










share|improve this question




















  • 2





    You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

    – HMR
    Nov 21 '18 at 16:02











  • I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

    – Emaborsa
    Nov 22 '18 at 7:00













  • No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

    – HMR
    Nov 22 '18 at 8:03






  • 2





    Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

    – Emaborsa
    Nov 22 '18 at 8:05











  • do you have null or 'null' as string in the array?

    – Nina Scholz
    Nov 22 '18 at 10:16
















2















I'm facing with a sorting problem in javascript. I wrote a simple sorting function where I want the null elements on top:



This is the function:



var mySort = function(a, b) {
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return -1;
if (a > b) return 1;
if (a < b) return -1;
return 0;
}


Let's take the following array as example:



var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];


By calling array.sort(mySort) the non null values are always sorted, but the null values position alternate from the beginning and the end of the array:



Odd calls: [null, null, 1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9]
Even calls: [1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9, null, null]


Why?



Edit:

In a comment HMR head me to an error in the algorithm: if (!b) return -1; should be if (!b) return 1;. Now it works fine with stings and numbers.










share|improve this question




















  • 2





    You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

    – HMR
    Nov 21 '18 at 16:02











  • I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

    – Emaborsa
    Nov 22 '18 at 7:00













  • No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

    – HMR
    Nov 22 '18 at 8:03






  • 2





    Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

    – Emaborsa
    Nov 22 '18 at 8:05











  • do you have null or 'null' as string in the array?

    – Nina Scholz
    Nov 22 '18 at 10:16














2












2








2








I'm facing with a sorting problem in javascript. I wrote a simple sorting function where I want the null elements on top:



This is the function:



var mySort = function(a, b) {
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return -1;
if (a > b) return 1;
if (a < b) return -1;
return 0;
}


Let's take the following array as example:



var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];


By calling array.sort(mySort) the non null values are always sorted, but the null values position alternate from the beginning and the end of the array:



Odd calls: [null, null, 1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9]
Even calls: [1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9, null, null]


Why?



Edit:

In a comment HMR head me to an error in the algorithm: if (!b) return -1; should be if (!b) return 1;. Now it works fine with stings and numbers.










share|improve this question
















I'm facing with a sorting problem in javascript. I wrote a simple sorting function where I want the null elements on top:



This is the function:



var mySort = function(a, b) {
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return -1;
if (a > b) return 1;
if (a < b) return -1;
return 0;
}


Let's take the following array as example:



var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];


By calling array.sort(mySort) the non null values are always sorted, but the null values position alternate from the beginning and the end of the array:



Odd calls: [null, null, 1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9]
Even calls: [1, 2, 2, 3, 3, 4, 4, 5, 6, 8, 8, 9, 9, null, null]


Why?



Edit:

In a comment HMR head me to an error in the algorithm: if (!b) return -1; should be if (!b) return 1;. Now it works fine with stings and numbers.







javascript sorting null






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:40







Emaborsa

















asked Nov 21 '18 at 15:53









EmaborsaEmaborsa

95331133




95331133








  • 2





    You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

    – HMR
    Nov 21 '18 at 16:02











  • I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

    – Emaborsa
    Nov 22 '18 at 7:00













  • No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

    – HMR
    Nov 22 '18 at 8:03






  • 2





    Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

    – Emaborsa
    Nov 22 '18 at 8:05











  • do you have null or 'null' as string in the array?

    – Nina Scholz
    Nov 22 '18 at 10:16














  • 2





    You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

    – HMR
    Nov 21 '18 at 16:02











  • I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

    – Emaborsa
    Nov 22 '18 at 7:00













  • No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

    – HMR
    Nov 22 '18 at 8:03






  • 2





    Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

    – Emaborsa
    Nov 22 '18 at 8:05











  • do you have null or 'null' as string in the array?

    – Nina Scholz
    Nov 22 '18 at 10:16








2




2





You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

– HMR
Nov 21 '18 at 16:02





You can call it as many times as you want if you do: array.slice().sort(mySort) if one item is falsy (null) it's either put on the start or the end but if they're both null then none of the items are moved. So the nulls will group at the start or the end. If you do if (!b) return 1; then it'll also always be the same.

– HMR
Nov 21 '18 at 16:02













I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

– Emaborsa
Nov 22 '18 at 7:00







I don't really get the point of your answer. I already know about slicing the array, but IMHO it is a hack. I'd like to know why the null are switched even if the function is the same...it should be always the same result...

– Emaborsa
Nov 22 '18 at 7:00















No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

– HMR
Nov 22 '18 at 8:03





No it doesn't. sort mutates and you return -1 for cases where a is null and b is not or vice versa, this causes null's to either be at the start or at the end depending on the order sort will compare the items and where the items were to start with. You could either not mutate the array (use slice) or return a different value when one is null.

– HMR
Nov 22 '18 at 8:03




2




2





Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

– Emaborsa
Nov 22 '18 at 8:05





Damn.... if (!b) return -1; is wrong, it should be if (!b) return 1; Now it works fine!.Thanks!

– Emaborsa
Nov 22 '18 at 8:05













do you have null or 'null' as string in the array?

– Nina Scholz
Nov 22 '18 at 10:16





do you have null or 'null' as string in the array?

– Nina Scholz
Nov 22 '18 at 10:16












1 Answer
1






active

oldest

votes


















1














You need to use the relation between two items. If both items are null, then change nothing, if you have one null value, then it depends on the position.



The best way is to check the items and get the delta of the check for sorting.






function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer


























  • I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

    – Emaborsa
    Nov 22 '18 at 6:58











  • it keeps the order. on which user agent is this running?

    – Nina Scholz
    Nov 22 '18 at 7:04











  • Chrome Version 70.0.3538.102

    – Emaborsa
    Nov 22 '18 at 7:07











  • it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

    – Nina Scholz
    Nov 22 '18 at 7:09











  • Ok, but I need something that works on (almost) every browser.

    – Emaborsa
    Nov 22 '18 at 7:11











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%2f53415829%2fdifferent-output-calling-javascript-sort-function-twice%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to use the relation between two items. If both items are null, then change nothing, if you have one null value, then it depends on the position.



The best way is to check the items and get the delta of the check for sorting.






function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer


























  • I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

    – Emaborsa
    Nov 22 '18 at 6:58











  • it keeps the order. on which user agent is this running?

    – Nina Scholz
    Nov 22 '18 at 7:04











  • Chrome Version 70.0.3538.102

    – Emaborsa
    Nov 22 '18 at 7:07











  • it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

    – Nina Scholz
    Nov 22 '18 at 7:09











  • Ok, but I need something that works on (almost) every browser.

    – Emaborsa
    Nov 22 '18 at 7:11
















1














You need to use the relation between two items. If both items are null, then change nothing, if you have one null value, then it depends on the position.



The best way is to check the items and get the delta of the check for sorting.






function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer


























  • I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

    – Emaborsa
    Nov 22 '18 at 6:58











  • it keeps the order. on which user agent is this running?

    – Nina Scholz
    Nov 22 '18 at 7:04











  • Chrome Version 70.0.3538.102

    – Emaborsa
    Nov 22 '18 at 7:07











  • it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

    – Nina Scholz
    Nov 22 '18 at 7:09











  • Ok, but I need something that works on (almost) every browser.

    – Emaborsa
    Nov 22 '18 at 7:11














1












1








1







You need to use the relation between two items. If both items are null, then change nothing, if you have one null value, then it depends on the position.



The best way is to check the items and get the delta of the check for sorting.






function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer















You need to use the relation between two items. If both items are null, then change nothing, if you have one null value, then it depends on the position.



The best way is to check the items and get the delta of the check for sorting.






function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }








function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }





function mySort(a, b) {
return (b === null) - (a === null) || a - b;
}

var array = [1, 9, 4, 8, null, 2, 3, 4, null, 6, 3, 2, 8, 9, 5];

array.sort(mySort);
console.log(array);

array.sort(mySort);
console.log(array);

.as-console-wrapper { max-height: 100% !important; top: 0; }






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 7:04

























answered Nov 21 '18 at 16:02









Nina ScholzNina Scholz

178k1491159




178k1491159













  • I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

    – Emaborsa
    Nov 22 '18 at 6:58











  • it keeps the order. on which user agent is this running?

    – Nina Scholz
    Nov 22 '18 at 7:04











  • Chrome Version 70.0.3538.102

    – Emaborsa
    Nov 22 '18 at 7:07











  • it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

    – Nina Scholz
    Nov 22 '18 at 7:09











  • Ok, but I need something that works on (almost) every browser.

    – Emaborsa
    Nov 22 '18 at 7:11



















  • I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

    – Emaborsa
    Nov 22 '18 at 6:58











  • it keeps the order. on which user agent is this running?

    – Nina Scholz
    Nov 22 '18 at 7:04











  • Chrome Version 70.0.3538.102

    – Emaborsa
    Nov 22 '18 at 7:07











  • it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

    – Nina Scholz
    Nov 22 '18 at 7:09











  • Ok, but I need something that works on (almost) every browser.

    – Emaborsa
    Nov 22 '18 at 7:11

















I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

– Emaborsa
Nov 22 '18 at 6:58





I copied your example, it behavies as mine does. Try to call the sort a second time, the null values will go at the end.

– Emaborsa
Nov 22 '18 at 6:58













it keeps the order. on which user agent is this running?

– Nina Scholz
Nov 22 '18 at 7:04





it keeps the order. on which user agent is this running?

– Nina Scholz
Nov 22 '18 at 7:04













Chrome Version 70.0.3538.102

– Emaborsa
Nov 22 '18 at 7:07





Chrome Version 70.0.3538.102

– Emaborsa
Nov 22 '18 at 7:07













it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

– Nina Scholz
Nov 22 '18 at 7:09





it is working fine with 70.0.3538.102 and the very actual version 70.0.3538.110 on windows.

– Nina Scholz
Nov 22 '18 at 7:09













Ok, but I need something that works on (almost) every browser.

– Emaborsa
Nov 22 '18 at 7:11





Ok, but I need something that works on (almost) every browser.

– Emaborsa
Nov 22 '18 at 7:11


















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%2f53415829%2fdifferent-output-calling-javascript-sort-function-twice%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

Costa Masnaga

Fotorealismo

Sidney Franklin