3

I need to sort an array of this format, using plain old Javascript.

var arr = ["0.01 BASIC", "0.01 SEF", "0.015 BASIC"];

What I need is for the array to be sorted first by decimal then by string and produce an output just like below;

arr = ["0.01 BASIC", "0.015 BASIC", "0.01 SEF"];

I cannot use jquery in performing the sort. Just plain good old Javascript.

4
  • Plain old JS has a sort function. Have a look here developer.mozilla.org/en-US/docs/JavaScript/Reference/… Commented Apr 9, 2013 at 6:47
  • 2
    @elclanrs Plain old JS has a sort function true, but it will not fulfill the requirement he specified for sorting with precedence. Commented Apr 9, 2013 at 6:48
  • 1
    @FeistyMango: Sure you can, you just need to get creative and try it out. There might be other ways but you can use sort to fulfill the requirement. Commented Apr 9, 2013 at 6:54
  • @elclanrs See below... Commented Apr 9, 2013 at 6:57

3 Answers 3

6

You can do this :

arr.sort(function(a,b){
   var at = a.split(' '), bt = b.split(' ');
   if (at[1]!=bt[1]) return at[1]>bt[1] ? 1 : -1;
   return parseFloat(at[0])-parseFloat(bt[0]);
});

If you want to sort a very big array, it might be faster to cache the keys. But it probably doesn't matter for most arrays.

Example :

["0.01 BASIC", "0.01 SEF", "0.015 BASIC", "0.2 BASIC", "0.001 SEF", "0.2 AAA"]
->
["0.2 AAA", "0.01 BASIC", "0.015 BASIC", "0.2 BASIC", "0.001 SEF", "0.01 SEF"] 
Sign up to request clarification or add additional context in comments.

2 Comments

There! a solution using sort. Nice.
@elclanrs lol, this has really very little to do with using the sort() function and everything to do with dystroy implementing the meat and potatoes. The sort() function at this point is just a glorified for loop.
1

two consecutive sorts will work:

arr.sort(function(a,b){
  // sort by words first
  return a.match(/[a-z]+/i).join("")<b.match(/[a-z]+/i).join()?-1:1;
}).sort(function(a,b){
  // sort by numbers next, but only if the words are equal
  return a.match(/[a-z]+/i).join("")!==b.match(/[a-z]+/i).join("")?0:parseInt(a)<parseInt(b)?-1:1;
})

4 Comments

I don't think ECMAScript's sort is guaranteed to be stable.
@dystroy what exactly do you mean?
I mean the the second sort can legally totally delete the order given by the first one.
well that goes without saying but i thought got that covered by comparing the strings in the second sort
1

using plain old Javascript : FIDDLE

    var xOriginal = ["0.01 BASIC", "0.015 BASIC", "0.01 SEF"];
    var xTemp = "";

for(var i=0;i<=xOriginal.length-1;i++)
{
for(var j=1;j<=xOriginal.length-1;j++)
{

 var xArr = xOriginal[i].split(" ");
 var yArr = xOriginal[j].split(" ");

  if((xArr[0] > yArr[0]) && (xArr[1] > yArr[1])) 
            {
               xTemp = xOriginal[i];
               xOriginal[i] = xOriginal[j];
               xOriginal[j] = xTemp;
            }


}
}

alert(xOriginal);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.