Sort function to sort item numbers separated by colon Below is my code. This is a function that will be applied as a sorting method to a grid based data set. The data in the column to be sorted can be stuff like: 123456, 123456:123444, and 12345:12359:139943. So far, I have tested it and it works, but I want to know in which situations might it not work, or if there is a way to optimize/improve the method in any way.

function SkuSorter(a, b, c) {
    // check if either input is a set
    var aMatch = a.match(/\:/g);
    var bMatch = b.match(/\:/g);

    // all items by default should be a regular sku, we'll change this later
    var aType = 0;
    var bType = 0;

    /* types;
     * 0 = regular sku
     * 1 = duo (12345:12345)
     * 2 = trio (1234:1234:1234)
     */

    // has the user sorted asc or desc? 
    var direction = c;


    // if 'a' is a set, determine if it is a duo or trio
    if (aMatch) {
        if (aMatch.length == 1) aType = 1;

        if (aMatch.length &gt; 1) aType = 2;
    }

    // save as above
    if (bMatch) {
        if (bMatch.length == 1) bType = 1;

        if (bMatch.length &gt; 1) bType = 2;
    }

    // if 'a' is a set and 'b' isn't, a should come before b (puts sets at the bottom)
    if (aType == 0 &amp;&amp; bType != 0) {
        return -1 * direction;
    }

    // save as above, but viceversa.
    if (aType != 0 &amp;&amp; bType == 0) {
        return 1 * direction;
    }

    // if both inputs are regular skus, do normal sort operation
    if (aType == 0 &amp;&amp; bType == 0) {
        if (direction == -1) {
            return b - a;
        } else {
            return a - b;
        }
    }

    // if 'a' is a set and it is of higher order (i.e. trio &gt; duo)
    if (aType &gt; 0 &amp;&amp; aType &gt; bType) {
        return 1 * direction;
    }

    // same as above, but for 'b'
    if (bType &gt; 0 &amp;&amp; bType &gt; aType) {
        return -1 * direction;
    }

    // if both inputs are sets
    if (aType &gt; 0 &amp;&amp; bType &gt; 0) {

        //break up the individual pieces
        var aP = a.split(':');
        var bP = b.split(':');

        /*
         * The following conditions will compare:
         * The first chunk and sort them based on value
         * Then the second chunk, and if it is a trio, the third.
         */
        if (aP[0] &gt; bP[0]) {
            return 1 * direction;
        }

        if (aP[0] &lt; bP[0]) {
            return -1 * direction;
        }

        // second chunk
        if (aP[1] &gt; bP[1]) {
            return 1 * direction;
        }

        if (aP[1] &lt; bP[1]) {
            return -1 * direction;
        }

        // third chunk
        if (aType &gt; 1 &amp;&amp; bType &gt; 1) {
            if (aP[2] &gt; bP[2]) {
                return 1 * direction;
            }

            if (aP[2] &lt; bP[2]) {
                return -1 * direction;
            }
        }
    }
}


Expected behavior:

Before:

1654110
1574698
1189364
1229764
1700004
310425
1626613
36509
1676618
1676832
1536622
1749548
36509:310444
3199:44999:34000
3199:45000:34000
3199:45000:34111
1874361
1551225
1581271
1626076
36509:310425
1676816
1676824


After:

36509
310425
1189364
1229764
1536622
1551225
1574698
1581271
1626076
1626613
1654110
1676618
1676816
1676824
1676832
1700004
1749548
1874361
36509:310425
36509:310444
3199:44999:34000
3199:45000:34000
3199:45000:34111
@Flambino was a bit faster : 

function sortSKU( a, b  )
{
    var aParts = a.split( ':' ),
        bParts = b.split( ':' ),
        partCount = aParts.length,
        i;

    if( aParts.length != bParts.length )
        return aParts.length - bParts.length;

    for( i = 0 ; i &lt; partCount ; i++ )
    {
      if( aParts[i] != bParts[i] )
        return +aParts[i] - +bParts[i];
    }
    //Exactly the same
    return 0;
}

console.log( data.sort( sortSKU )  );


As Flambino said, use .reverse() to sort one way or another. Also this function will sort 123 prior to 2:2 if such a case can occur.

If you need the 'c'

function sortingCallback( array, direction ){

  if( direction == 'whatever ascending is' ){
    return array.sort( sortSKU );
  }

  return array.sort( sortSKU ).reverse();
}