5

Hi I want to sort the string based on numbers present in it e.g

  • 1.1.act2
  • 13.1.2.1.act3
  • 2.1.4.act56
  • 1.3.actorg
  • 3.1.3.args
  • 13.1.3.4.acr

I want to this in a manner

  • 1.1.act2
  • 1.3.actorg
  • 2.1.4.act56
  • 3.1.3.args
  • 13.1.2.1.act3
  • 13.1.3.4.acry

How do I achieve it in javascript?

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
BaD aNgEl
  • 75
  • 7
  • You can use answers from [this question](http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number) to build a `compare` function for `array.sort()` – Shawn Chin Oct 29 '12 at 10:35

2 Answers2

3

You can use the Arrays sort method with a custom iterator function. e.g:

mylist = ["1.1.act2", "13.1.2.1.act3", "2.1.4.act56", "1.3.actorg", "3.1.3.args", "13.1.3.4.acr"];

mylist.sort(function(a, b) {
    a = a.split("."); b = b.split(".");
    var parts = Math.min(a.length, b.length);
    for(var i = 0; i < parts; ++i) {
        var numA = parseInt(a[i]); var numB = parseInt(b[i]);
        if (numA != numB)
            return numA > numB;
    }
});

This is a stub and untested, but I guess you see where I'm heading

Constantinius
  • 34,183
  • 8
  • 77
  • 85
-2

Try this. This code is not optimized but it works

    <script type="text/javascript">
function fnc()
{
    var arr=new Array();
arr[0]="1.1.act2";
arr[1]="13.1.2.1.act3";
arr[2]="2.1.4.act56";
arr[3]="1.3.actorg";
arr[4]="3.1.3.args";
arr[5]="13.1.3.4.acr";

var it0=arr[0].split(".");
var it1=arr[1].split(".");
var it2=arr[2].split(".");
var it3=arr[3].split(".");
var it4=arr[4].split(".");
var it5=arr[5].split(".");

var newarr=new Array();
newarr[0]=parseInt(it0[0],10);
newarr[1]=parseInt(it1[0],10);
newarr[2]=parseInt(it2[0],10);
newarr[3]=parseInt(it3[0],10);
newarr[4]=parseInt(it4[0],10);
newarr[5]=parseInt(it5[0],10);

newarr.sort(function(a,b){return a-b});
alert(newarr);
for(i=0;i<arr.length;i++)
{

    if(newarr[i]==it0[0])
    {console.log(arr[0]);}
    else if(newarr[i]==it1[0])
    {console.log(arr[1]);}
    else if(newarr[i]==it2[0])
    {console.log(arr[2]);}
    else if(newarr[i]==it3[0])
    {console.log(arr[3]);}
    else if(newarr[i]==it4[0])
    {console.log(arr[4]);}
    else if(newarr[i]==it5[0])
    {console.log(arr[5]);}
    else if(newarr[i]==it6[0])
    {console.log(arr[6]);}
}
}
<input type="button" onclick="fnc()" value="click" />
polin
  • 2,745
  • 2
  • 15
  • 20
  • This will only work for the first number, and only for 6 entries. – Constantinius Oct 29 '12 at 11:31
  • obviously it'll work for only 6 entries. I've shown the process.You shouldn't expect me to do the whole process. Copy and paste and see how it works. Its easier to vote negative without judging. – polin Oct 29 '12 at 11:37
  • That is a non-generic solution. If you explicitness access each value, you can just sort them already by swapping them. – Mortalus Oct 29 '12 at 11:49
  • What do you mean by non generic solution.I works perfectly. what you need to do push the items into an array first. Have you tried my code? Try it and see in on firebug – polin Oct 29 '12 at 11:55
  • @polin: I trust that your code works for the set of data given. But it is not generic in a sense, that you can use it on any kind of data following a specific scheme. Also the code is quite verbose for the task at hand. "*Its easier to vote negative without judging.*" - I did inspect your code quite thoroughly, be assured, but it simply lacks utility. – Constantinius Oct 29 '12 at 13:04
  • @polin: Also, it does possibly not provide correct results, as it only does take into account the *first* number the sequence, so there is no *strong* ordering of the elements. It may work for the few input strings at hand, but maybe not for another set of data. – Constantinius Oct 29 '12 at 13:11