Skip to content

Instantly share code, notes, and snippets.

@fatgy
Created December 14, 2010 10:39
Show Gist options
  • Save fatgy/740253 to your computer and use it in GitHub Desktop.
Save fatgy/740253 to your computer and use it in GitHub Desktop.
Javascript sort Object by value
/*
http://nemojkliknut.com/blog/javascript-sort-object-by-value/55
a simple bubble sort algorithm on an object
*/
function bubbleSortObject(object) {
for (i in object) {
for (k in object) {
if (object[i] < object[k]) {
var temp = object[i];
object[i] = object[k];
object[k] = temp;
}
}
}
return object;
}
var numbers = new Object({'item1': 12345, 'item2': 98765, 'item3': 0.00001});
var result = bubbleSortObject(numbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment