snippetjavascriptMinor
Sort function to sort item numbers separated by colon
Viewed 0 times
itemnumbersfunctionseparatedsortcolon
Problem
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:
```
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 > 1) aType = 2;
}
// save as above
if (bMatch) {
if (bMatch.length == 1) bType = 1;
if (bMatch.length > 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 && bType != 0) {
return -1 * direction;
}
// save as above, but viceversa.
if (aType != 0 && bType == 0) {
return 1 * direction;
}
// if both inputs are regular skus, do normal sort operation
if (aType == 0 && 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 > duo)
if (aType > 0 && aType > bType) {
return 1 * direction;
}
// same as above, but for 'b'
if (bType > 0 && bType > aType) {
return -1 * direction;
}
// if both inputs are sets
if (aType > 0 && bType > 0) {
//break up the individual pieces
var aP = a.split(':');
var bP = b.
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 > 1) aType = 2;
}
// save as above
if (bMatch) {
if (bMatch.length == 1) bType = 1;
if (bMatch.length > 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 && bType != 0) {
return -1 * direction;
}
// save as above, but viceversa.
if (aType != 0 && bType == 0) {
return 1 * direction;
}
// if both inputs are regular skus, do normal sort operation
if (aType == 0 && 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 > duo)
if (aType > 0 && aType > bType) {
return 1 * direction;
}
// same as above, but for 'b'
if (bType > 0 && bType > aType) {
return -1 * direction;
}
// if both inputs are sets
if (aType > 0 && bType > 0) {
//break up the individual pieces
var aP = a.split(':');
var bP = b.
Solution
@Flambino was a bit faster :
As Flambino said, use
If you need the 'c'
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 < 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();
}Code Snippets
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 < partCount ; i++ )
{
if( aParts[i] != bParts[i] )
return +aParts[i] - +bParts[i];
}
//Exactly the same
return 0;
}
console.log( data.sort( sortSKU ) );function sortingCallback( array, direction ){
if( direction == 'whatever ascending is' ){
return array.sort( sortSKU );
}
return array.sort( sortSKU ).reverse();
}Context
StackExchange Code Review Q#47366, answer score: 3
Revisions (0)
No revisions yet.