Skip to content

Instantly share code, notes, and snippets.

@danielsharvey
Created January 30, 2017 03:38
Show Gist options
  • Save danielsharvey/6013d4c41b8b934fb67c447358017b5f to your computer and use it in GitHub Desktop.
Save danielsharvey/6013d4c41b8b934fb67c447358017b5f to your computer and use it in GitHub Desktop.
Javascript merge function
function merge(left, right, sortFieldName){
var ret = []
var il = 0
var ir = 0
while (il < left.length && ir < right.length) {
if (left[il][sortFieldName].valueOf() < right[ir][sortFieldName].valueOf()) {
ret.push(left[il++])
} else {
ret.push(right[ir++])
}
}
while(il<left.length) ret.push(left[il++])
while(ir<right.length) ret.push(right[ir++])
return ret // ret.concat(left.slice(il)).concat(right.slice(ir));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment