using merge sort / quick sort
Merge Sort\n\n/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */// merge sort\nvar sortBy = function(arr, fn) {\n  const n = arr.lengt
# Merge Sort\n```\n/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */// merge sort\nvar sortBy = function(arr, fn) {\n  const n = arr.length;\n  // base case\n  if (n == 1) return arr;\n\n  // divide\n  const mid = Math.floor(n/2);\n  const arrLeft = arr.slice(0, mid); \n  const arrRight = arr.slice(mid, n);\n\n  // sort and merge \n  return merge(sortBy(arrLeft, fn), sortBy(arrRight, fn), fn);\n};\n\n/**\n * @param {Array} left\n * @param {Array} right\n * @param {Function} fn\n * @return {Array}\n */// merge function\nfunction merge (left, right, fn) {\n  const sortedArray = [];\n  while (left.length > 0 && right.length > 0) {\n    // sort and merge\n    if (fn(left[0]) < fn(right[0])) {\n      sortedArray.push(left[0]);\n      left.shift();\n    } else {\n      sortedArray.push(right[0]);\n      right.shift();\n    }\n  }\n\n  return sortedArray.concat(left, right);\n}\n```\n# Quick Sort\n```\n/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */// using quick sort\nvar sortBy = function(arr, fn) {\n  // function quick sort\n  const quickSort = (arr, leftBound = 0, rightBound = arr.length-1) => {\n    if (leftBound < rightBound) {\n      const pivotIdx = partition(arr, leftBound, rightBound);\n      quickSort(arr, leftBound,pivotIdx-1);\n      quickSort(arr, pivotIdx, rightBound);\n    }\n    return arr;\n  }\n\n  // function partition\n  const partition = (arr, left, right) => {\n    const pivot = fn(arr[Math.floor((right+left) /2)]);\n    while (left <= right) {\n      while (fn(arr[left]) < pivot) left++;\n      while (fn(arr[right]) > pivot) right--;\n\n      if (left <= right) {\n        // swap\n        const temp = arr[right];\n        arr[right] = arr[left];\n        arr[left] = temp;\n\n        left++;\n        right--;\n      }\n    }\n\n    return left;\n  }\n\n  return quickSort(arr);\n}\n```
['Sorting', 'Merge Sort', 'JavaScript']