Implemented the merge sort algorithm in my javascript code.
I'm wonder how I can target specific attributes like date
, title
, name
etc for sorting in an array when calling merge sort like mergeSort(array);
.
function mergeSort(arr){
var len = arr.length;
if(len <2)
return arr;
var mid = Math.floor(len/2),
left = arr.slice(0,mid),
right =arr.slice(mid);
return merge(mergeSort(left),mergeSort(right));
}
function merge(left, right){
var result = [],
lLen = left.length,
rLen = right.length,
l = 0,
r = 0;
while(l < lLen && r < rLen){
if(left[l] < right[r]){
result.push(left[l++]);
}
else{
result.push(right[r++]);
}
}
return result.concat(left.slice(l)).concat(right.slice(r));
}
Using it in a sort options method. What I want is to print a sorted list. The way the list is sorted will be defined by the users chosen sort option.
function sortConfig(array, sortOption){
if(sortOption == 'title') mergeSort(array.Title);
//..etc
}
sort
function. - le_msortConfig
is, norarray
- Don't just show us your function, show us the parameters - What is the expected results vs. the actual, show us how you call those functions - Just add more context - Alon Eitan(item) => item.title
- Icepickle