Javascript and ordering

Is there a simple way, a library maybe, for sorting?  I have numeric values I need to order.
LVL 1
HLRosenbergerAsked:
Who is Participating?
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.

Alexandre SimõesManager / 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);

Open in new window


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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
0
Alexandre SimõesManager / 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.
0
HLRosenbergerAuthor Commented:
Thanks!  that does the trick.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.