简体   繁体   中英

JS Merge Sort that sorts by a Object value of choice

Lets get right to it, this is my Code:

function mergeSort(unsortedArray, sortingParameter) {
  if (unsortedArray.length <= 1) {
    return unsortedArray;
  }

  const middle = Math.floor(unsortedArray.length / 2);

  const left = unsortedArray.slice(0, middle);
  const right = unsortedArray.slice(middle);

  // Using recursion to combine the left and right

  return merge(mergeSort(left), mergeSort(right), sortingParameter);
}

function merge(left, right, sortingParameter) {
  let resultArray = [],
    leftIndex = 0,
    rightIndex = 0;

  if (sortingParameter) {
    console.log(left[2][sortingParameter], "parameterCheck");
    while (leftIndex < left.length && rightIndex < right.length) {
      console.log("while");
      if (
        left[leftIndex][sortingParameter] < right[rightIndex][sortingParameter]
      ) {
        resultArray.push(left[leftIndex]);
        leftIndex++;
      } else {
        resultArray.push(right[rightIndex]);
        rightIndex++;
      }
    }
  } else {
    while (leftIndex < left.length && rightIndex < right.length) {
      if (left[leftIndex] < right[rightIndex]) {
        resultArray.push(left[leftIndex]);
        leftIndex++;
      } else {
        resultArray.push(right[rightIndex]);
        rightIndex++;
      }
    }
  }

  return resultArray
    .concat(left.slice(leftIndex))
    .concat(right.slice(rightIndex));
}

export { mergeSort };

I am new to algorithms so I am not sure it's not an obvious error, but I am trying to sort a comments Object structured like this

{
likeCount: integer,
prop: "moreStuff"
}

By the integer value stored at the likeCount key... when I put in an Array of Integers without a sorting parameter this function works just fine for example

[9,5,7,3,1] => [1,3,5,7,9]

This is a big headache for me thank you for the help in advance!!!

You forgot to pass the sortingParameter in recursive call of mergeSort, see snippet

 function mergeSort(unsortedArray, sortingParameter) { if (unsortedArray.length <= 1) { return unsortedArray; } const middle = Math.floor(unsortedArray.length / 2); const left = unsortedArray.slice(0, middle); const right = unsortedArray.slice(middle); // Using recursion to combine the left and right return merge(mergeSort(left, sortingParameter), mergeSort(right, sortingParameter), sortingParameter); } function merge(left, right, sortingParameter) { let resultArray = [], leftIndex = 0, rightIndex = 0; if (sortingParameter) { while (leftIndex < left.length && rightIndex < right.length) { if (left[leftIndex][sortingParameter] < right[rightIndex][sortingParameter]) { resultArray.push(left[leftIndex]); leftIndex++; } else { resultArray.push(right[rightIndex]); rightIndex++; } } } else { while (leftIndex < left.length && rightIndex < right.length) { if (left[leftIndex] < right[rightIndex]) { resultArray.push(left[leftIndex]); leftIndex++; } else { resultArray.push(right[rightIndex]); rightIndex++; } } } return resultArray.concat(left.slice(leftIndex)).concat(right.slice(rightIndex)); } console.log(mergeSort([{ count: 4, val: "A" }, { count: 7, val: "B" }, { count: 9, val: "C" }, { count: 12, val: "D" }, { count: 3, val: "E" }, { count: 6, val: "F" }, { count: 7, val: "G" }, { count: 5, val: "H" }], "count"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM