Skip to content

Instantly share code, notes, and snippets.

@bytao7mao
Created August 14, 2017 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytao7mao/1784313ba8aba948325a5ec661c6f55d to your computer and use it in GitHub Desktop.
Save bytao7mao/1784313ba8aba948325a5ec661c6f55d to your computer and use it in GitHub Desktop.
JS push and sort
function getIndexToIns(arr, num) {
arr.push(num); // first we add a number in the array
arr.sort(function(a, b) { // then we sort the array in ascending order
return a-b;
});
return arr.indexOf(num); //then we return the index of the num added to the array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment