How to sort elements in array without changing other elements indexes? [duplicate]












14
















This question already has an answer here:




  • How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

    6 answers




I have this array:



var arr = [5, 3, 2, 8, 1, 4];


I'm trying to sort ONLY the elements that are odd values so I want this



output:



[1, 3, 2, 8, 5, 4]


As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:






function myFunction(array) {

var oddElements = array.reduce((arr, val, index) => {
if (val % 2 !== 0){
arr.push(val);
}
return arr.sort();
}, );

return oddElements;
}
console.log(myFunction([5, 3, 2, 8, 1, 4]));





I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.










share|improve this question















marked as duplicate by Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    14
















    This question already has an answer here:




    • How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

      6 answers




    I have this array:



    var arr = [5, 3, 2, 8, 1, 4];


    I'm trying to sort ONLY the elements that are odd values so I want this



    output:



    [1, 3, 2, 8, 5, 4]


    As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:






    function myFunction(array) {

    var oddElements = array.reduce((arr, val, index) => {
    if (val % 2 !== 0){
    arr.push(val);
    }
    return arr.sort();
    }, );

    return oddElements;
    }
    console.log(myFunction([5, 3, 2, 8, 1, 4]));





    I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.










    share|improve this question















    marked as duplicate by Nina Scholz javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    2 days ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      14












      14








      14









      This question already has an answer here:




      • How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

        6 answers




      I have this array:



      var arr = [5, 3, 2, 8, 1, 4];


      I'm trying to sort ONLY the elements that are odd values so I want this



      output:



      [1, 3, 2, 8, 5, 4]


      As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:






      function myFunction(array) {

      var oddElements = array.reduce((arr, val, index) => {
      if (val % 2 !== 0){
      arr.push(val);
      }
      return arr.sort();
      }, );

      return oddElements;
      }
      console.log(myFunction([5, 3, 2, 8, 1, 4]));





      I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.










      share|improve this question

















      This question already has an answer here:




      • How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

        6 answers




      I have this array:



      var arr = [5, 3, 2, 8, 1, 4];


      I'm trying to sort ONLY the elements that are odd values so I want this



      output:



      [1, 3, 2, 8, 5, 4]


      As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:






      function myFunction(array) {

      var oddElements = array.reduce((arr, val, index) => {
      if (val % 2 !== 0){
      arr.push(val);
      }
      return arr.sort();
      }, );

      return oddElements;
      }
      console.log(myFunction([5, 3, 2, 8, 1, 4]));





      I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.





      This question already has an answer here:




      • How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

        6 answers







      function myFunction(array) {

      var oddElements = array.reduce((arr, val, index) => {
      if (val % 2 !== 0){
      arr.push(val);
      }
      return arr.sort();
      }, );

      return oddElements;
      }
      console.log(myFunction([5, 3, 2, 8, 1, 4]));





      function myFunction(array) {

      var oddElements = array.reduce((arr, val, index) => {
      if (val % 2 !== 0){
      arr.push(val);
      }
      return arr.sort();
      }, );

      return oddElements;
      }
      console.log(myFunction([5, 3, 2, 8, 1, 4]));






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago







      progx

















      asked 2 days ago









      progxprogx

      331522




      331522




      marked as duplicate by Nina Scholz javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Nina Scholz javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          5














          One option is to keep track of the indicies of the odd numbers in the original array, and after .reduceing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:






          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));








          share|improve this answer


























          • your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

            – CertainPerformance
            2 days ago











          • Thanks a lot bro!

            – progx
            2 days ago



















          8














          First sort only the odd numbers and put it in an array oddSorted. Then map through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted array.






          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));








          share|improve this answer


























          • easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

            – user633183
            2 days ago













          • One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

            – Amardeep Bhowmick
            2 days ago











          • You don't need to clone arr. Filter will return a new array anyway.

            – kremerd
            2 days ago



















          0














          I modified your code a little bit to fulfill your objective. Take a look below






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()






          share|improve this answer


























          • Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Okay... I'm taking a look at it

            – Tanmoy Krishna Das
            2 days ago











          • The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

            – Tanmoy Krishna Das
            2 days ago


















          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          One option is to keep track of the indicies of the odd numbers in the original array, and after .reduceing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:






          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));








          share|improve this answer


























          • your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

            – CertainPerformance
            2 days ago











          • Thanks a lot bro!

            – progx
            2 days ago
















          5














          One option is to keep track of the indicies of the odd numbers in the original array, and after .reduceing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:






          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));








          share|improve this answer


























          • your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

            – CertainPerformance
            2 days ago











          • Thanks a lot bro!

            – progx
            2 days ago














          5












          5








          5







          One option is to keep track of the indicies of the odd numbers in the original array, and after .reduceing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:






          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));








          share|improve this answer















          One option is to keep track of the indicies of the odd numbers in the original array, and after .reduceing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:






          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));








          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));





          function oddSort(array) {
          const oddIndicies = ;
          const newArr = array.slice();
          const sortedOdd = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          oddIndicies.push(index);
          }
          return arr;
          }, )
          .sort((a, b) => a - b);
          while (oddIndicies.length > 0) {
          newArr[oddIndicies.shift()] = sortedOdd.shift();
          }
          return newArr;
          }

          console.log(oddSort([5, 3, 2, 8, 1, 4]));
          console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          CertainPerformanceCertainPerformance

          81.3k143865




          81.3k143865













          • your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

            – CertainPerformance
            2 days ago











          • Thanks a lot bro!

            – progx
            2 days ago



















          • your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

            – CertainPerformance
            2 days ago











          • Thanks a lot bro!

            – progx
            2 days ago

















          your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

          – progx
          2 days ago





          your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

          – progx
          2 days ago













          Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

          – CertainPerformance
          2 days ago





          Ah, the 11 was throwing things off, since .sort sorts lexiographically - use a custom .sort function instead, see edit

          – CertainPerformance
          2 days ago













          Thanks a lot bro!

          – progx
          2 days ago





          Thanks a lot bro!

          – progx
          2 days ago













          8














          First sort only the odd numbers and put it in an array oddSorted. Then map through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted array.






          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));








          share|improve this answer


























          • easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

            – user633183
            2 days ago













          • One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

            – Amardeep Bhowmick
            2 days ago











          • You don't need to clone arr. Filter will return a new array anyway.

            – kremerd
            2 days ago
















          8














          First sort only the odd numbers and put it in an array oddSorted. Then map through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted array.






          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));








          share|improve this answer


























          • easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

            – user633183
            2 days ago













          • One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

            – Amardeep Bhowmick
            2 days ago











          • You don't need to clone arr. Filter will return a new array anyway.

            – kremerd
            2 days ago














          8












          8








          8







          First sort only the odd numbers and put it in an array oddSorted. Then map through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted array.






          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));








          share|improve this answer















          First sort only the odd numbers and put it in an array oddSorted. Then map through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted array.






          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));








          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));





          function sortOddElements(arr){
          var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
          var evenNotSorted = arr.map((ele, idx) => {
          if(ele % 2 != 0){
          return oddSorted.shift();
          }
          return ele;
          });
          return evenNotSorted;
          }
          var arr = [5, 3, 2, 8, 1, 4];
          console.log(sortOddElements(arr));
          arr = [5, 3, 2, 8, 1, 4, 11 ];
          console.log(sortOddElements(arr));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Amardeep BhowmickAmardeep Bhowmick

          2,0351821




          2,0351821













          • easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

            – user633183
            2 days ago













          • One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

            – Amardeep Bhowmick
            2 days ago











          • You don't need to clone arr. Filter will return a new array anyway.

            – kremerd
            2 days ago



















          • easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

            – user633183
            2 days ago













          • One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

            – Amardeep Bhowmick
            2 days ago











          • You don't need to clone arr. Filter will return a new array anyway.

            – kremerd
            2 days ago

















          easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

          – user633183
          2 days ago







          easy-to-follow answer. now, can you think of a way to do this without having to do the % 2 != 0 calculation twice per element? :D

          – user633183
          2 days ago















          One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

          – Amardeep Bhowmick
          2 days ago





          One thing that comes to my mind is checking whether the current element is included in the oddSorted array, if yes then replace the current number with the first number from the oddSorted array. This will also work as if the current element is included in the oddSorted array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?

          – Amardeep Bhowmick
          2 days ago













          You don't need to clone arr. Filter will return a new array anyway.

          – kremerd
          2 days ago





          You don't need to clone arr. Filter will return a new array anyway.

          – kremerd
          2 days ago











          0














          I modified your code a little bit to fulfill your objective. Take a look below






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()






          share|improve this answer


























          • Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Okay... I'm taking a look at it

            – Tanmoy Krishna Das
            2 days ago











          • The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

            – Tanmoy Krishna Das
            2 days ago
















          0














          I modified your code a little bit to fulfill your objective. Take a look below






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()






          share|improve this answer


























          • Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Okay... I'm taking a look at it

            – Tanmoy Krishna Das
            2 days ago











          • The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

            – Tanmoy Krishna Das
            2 days ago














          0












          0








          0







          I modified your code a little bit to fulfill your objective. Take a look below






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()






          share|improve this answer















          I modified your code a little bit to fulfill your objective. Take a look below






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()






          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));





          function myFunction(array) {

          var oddElements = array.reduce((arr, val, index) => {
          if (val % 2 !== 0) {
          arr.push(val);
          }
          return arr.sort(function(a, b){return a - b});
          }, );

          var index = 0;
          var finalElements = ;
          for(var i=0; i<array.length; i++) {
          var element = array[i];
          if(element %2 !==0) {
          finalElements.push(oddElements[index]);
          index++;
          } else {
          finalElements.push(element);
          }
          }
          return finalElements;
          }
          console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Tanmoy Krishna DasTanmoy Krishna Das

          585213




          585213













          • Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Okay... I'm taking a look at it

            – Tanmoy Krishna Das
            2 days ago











          • The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

            – Tanmoy Krishna Das
            2 days ago



















          • Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

            – progx
            2 days ago











          • Okay... I'm taking a look at it

            – Tanmoy Krishna Das
            2 days ago











          • The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

            – Tanmoy Krishna Das
            2 days ago

















          Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

          – progx
          2 days ago





          Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]

          – progx
          2 days ago













          Okay... I'm taking a look at it

          – Tanmoy Krishna Das
          2 days ago





          Okay... I'm taking a look at it

          – Tanmoy Krishna Das
          2 days ago













          The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

          – Tanmoy Krishna Das
          2 days ago





          The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.

          – Tanmoy Krishna Das
          2 days ago



          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy