Skip to content

Instantly share code, notes, and snippets.

@razzul
Created January 20, 2017 08:33
Show Gist options
  • Save razzul/ba572e1060073da02f5e9425bf81e868 to your computer and use it in GitHub Desktop.
Save razzul/ba572e1060073da02f5e9425bf81e868 to your computer and use it in GitHub Desktop.
JavaScript : Sort array ascending | descend

By default the sort method sorts elements alphabetically.
To sort numerically just add a new method which handles numeric sorts

	var points = [40, 100, 1, 5, 25, 10];
	points.sort();
	// [1, 10, 100, 25, 40, 5]
	points.sort(function(a, b){return a-b})
	// [1, 5, 10, 25, 40, 100]
	points.sort(function(a, b){return b-a})
	// [100, 40, 25, 10, 5, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment