Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
Plain old JS has a sort function. Have a look here developer.mozilla.org/en-US/docs/JavaScript/Reference/… –  elclanrs Apr 9 '13 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. –  Feisty Mango Apr 9 '13 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. –  elclanrs Apr 9 '13 at 6:54
 
@elclanrs See below... –  Feisty Mango Apr 9 '13 at 6:57
add comment

3 Answers

up vote 6 down vote accepted

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"] 
share|improve this answer
 
Nicely done. Beat me to it =P –  Feisty Mango Apr 9 '13 at 6:55
 
There! a solution using sort. Nice. –  elclanrs Apr 9 '13 at 6:55
 
@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. –  Feisty Mango Apr 9 '13 at 6:57
 
Right, that's my point exactly xD. –  elclanrs Apr 9 '13 at 6:58
 
Works like magic. thank you very much! :) –  Ediste Dela Cruz Apr 9 '13 at 6:59
add comment

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;
})
share|improve this answer
1  
I don't think ECMAScript's sort is guaranteed to be stable. –  dystroy Apr 9 '13 at 6:56
 
@dystroy what exactly do you mean? –  lordvlad Apr 9 '13 at 6:58
 
I mean the the second sort can legally totally delete the order given by the first one. –  dystroy Apr 9 '13 at 6:59
 
well that goes without saying but i thought got that covered by comparing the strings in the second sort –  lordvlad Apr 9 '13 at 7:03
add comment

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.