I have a sort function that needed to sort in a certain way. That position was sorted by mobile-top first, then empty position followed by mobile-bottom. Then on these I needed to sort by their priority, with priority 1, 2 and then blank.
widgetInfo = [
['box1','mobile-bottom',''],
['box2','mobile-top','priority-1'],
['box3','mobile-top',''],
['box3','mobile-top',''],
['box4','mobile-bottom',''],
['box2','mobile-top','priority-1'],
['box5','mobile-top','priority-2'],
['box6','','']],
['box5','mobile-top','priority-2'];
var newArray = widgetInfo.sort(function(a, b){
if(a[1] == 'mobile-top'){
if(b[1] == 'mobile-bottom'){
return -1;
}
else if(b[1] == 'mobile-top'){
if(a[2] == 'priority-1'){
if(b[2] == 'priority-1'){
return 0;
}
else if(b[2] == 'priority-2'){
return -1;
}
else{
return -1;
}
}
else if(a[2] == 'priority-2'){
if(b[2] == 'priority-1'){
return 1;
}
else if(b[2] == 'priority-2'){
return 0;
}
else{
return -1;
}
}
else{
return 1;
}
}
else{
return -1;
}
}
if(a[1] == 'mobile-bottom'){
if(b[1] == 'mobile-top'){
return 1;
}
else if(b[1] == 'mobile-bottom'){
return 0;
}
else{
return 1;
}
}
});
It seems like one heck of a lot of code to do this and I'm certain there will be better ways. Any suggestions?