# Javascript and ordering

Is there a simple way, a library maybe, for sorting?  I have numeric values I need to order.
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Manager / Technology SpecialistCommented:
Well, the library is for sure Underscore.js.
The thing is that it does a whole bunch of other stuff and if you only need the sorting then it might be a bit too much.

Otherwise you can always implement your own bubble sort algorithm.
I took the following example from here: http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/
``````var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];

function bubbleSort(a)
{
var swapped;
do {
swapped = false;
for (var i=0; i < a.length-1; i++) {
if (a[i] > a[i+1]) {
var temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = true;
}
}
} while (swapped);
}

bubbleSort(a);
console.log(a);
``````

Here's a working sample of this code: http://jsfiddle.net/AlexCode/p2Lowqkh/
Commented:
Just interested to know why the javascript sort method is not suitable?

http://www.w3schools.com/jsref/jsref_sort.asp

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Web Ninja at largeCommented:
if you just have a list of numbers you need to sort, check out the array's sort method:

http://www.w3schools.com/jsref/jsref_sort.asp
Manager / Technology SpecialistCommented:
True, I didn't read the "I have numeric values I need to order" :)

If they are numbers and actually defined as numbers (not strings containing numbers), then you can use the sort directly.
If what you have is an array of strings, the sort will be by text which will result differently from the numeric sort.
Author Commented:
Thanks!  that does the trick.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.