Home > Net >  Merge Sort, sorting dates properly but not integer values
Merge Sort, sorting dates properly but not integer values

Time:07-02

I am doing a coding challenge trying to learn merge sort and I have gotten my merge sort to handle dates properly but not an integer value. Currently it seems to outputting data at random.

EDIT: There are over a dozen different posts like the sample data listed. I am unable to sort each of those objects based on their votes from high to low

My Merge Sort Function:

function sortBy(array, key, descending = false) {
  const length = array.length;
  if (length === 1) {
    return array;
  } else if (length === 2) {
    const aValue = array[0][key];
    const bValue = array[1][key];
    if (bValue > aValue) {
      return array;
    }
    return [
      array[0],
      array[1],
    ];
  }

  const mid = Math.floor(length / 2);
  const firstHalf = array.slice(0, mid);
  const secondHalf = array.slice(mid, length);

  const arrayOne = sortBy(firstHalf, key);
  const arrayTwo = sortBy(secondHalf, key);

  const merged = [];
  while (arrayOne.length || arrayTwo.length) {
    if (!arrayOne.length) {
      merged.push(arrayTwo.shift());
      continue;
    }

    if (!arrayTwo.length) {
      merged.push(arrayOne.shift());
      continue;
    }

    const valueOne = arrayOne[0][key];
    const valueTwo = arrayTwo[0][key];
    if (valueOne <= valueTwo) {
      merged.push(arrayOne.shift());
    } else if (valueTwo < valueOne) {
      merged.push(arrayTwo.shift());
    }
  }

  return descending ? merged.reverse() : merged;
}

Sample Data

    [{
    created: '2016-03-07T05:24:40.340Z',
    details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
    title: 'Support triple backtick codeblocks',
    votes: 17,
  },]

CodePudding user response:

2 things I see just reading through the code:

  1. need to access different elements of the array in the last return statement
const length = array.length;
  if (length === 1) {
    return array;
  } else if (length === 2) {
    const aValue = array[0][key];
    const bValue = array[1][key];
    if (bValue > aValue) {
      return array;
    }
    return [
      array[0], // needs to be array[1]
      array[1], // needs to be array[0]
    ];
  }
  1. You need to be passing the third argument down everytime you recurse the function

    const arrayOne = sortBy(firstHalf, key, descending);

    const arrayTwo = sortBy(secondHalf, key, descending);

CodePudding user response:

Use .sort()

arrayOfObjects.sort((a, b) => b[key] - a[key])

const data = [{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 7
}, {
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 1
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 77
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 11
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 71
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 117
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 177
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 717
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 171
},{
  created: '2016-03-07T05:24:40.340Z',
  details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
  title: 'Support triple backtick codeblocks',
  votes: 0
}];

let output = data.sort((a, b) => b.votes - a.votes);

console.log(output);

  • Related