How can I partially sort an array in JavaScript, based on some conditions that I choose?
Given an array
var tab = [
{html: 'This is a test'},
{html: 'Locked item', locked: true},
{html: 'Another item'},
{html: 'Another one'}
//and 100 more items...
];
The 2nd items is called "locked", it should stays at the same place (2nd place) after the array is sorted
var tab = [
{html: 'Another item'},
{html: 'Locked item', locked: true},
{html: 'Another one'},
{html: 'This is a test'},
//and 100 more items...
];
This is the complete code I currently have:
var tab = [
{html: 'This is a test'},
{html: 'Locked item', locked: true},
{html: 'Another item'},
{html: 'Another one'}
//and more items...
];
tab.sort(function(a,b){
var aVal = a.html.toLowerCase();
var bVal = b.html.toLowerCase();
if (aVal===bVal)
return 0;
else
return aVal < bVal ? -1 : 1;
//how to handle locked items?
});
//desire output
/*var tab = [
{html: 'Another item'},
{html: 'Locked item', locked: true},
{html: 'Another one'},
{html: 'This is a test'},
//and more items...
];*/
console.log(tab);
See fiddle
Probably could be optimised, but removing all locked items and storing their initial positions, then adding them back into the sorted array should work:
var tab = [
{html: 'This is a test'},
{html: 'Locked item', locked: true},
{html: 'Another item'},
{html: 'Another one'}
//and more items...
];
var stored = {}, newTab = [];
for(var i = 0, iLimit = tab.length; i < iLimit; i++) {
if(tab[i].locked) {
stored[i] = tab[i];
} else {
newTab.push(tab[i]);
}
}
newTab.sort(function(a,b){
var aVal = a.html.toLowerCase();
var bVal = b.html.toLowerCase();
if (aVal===bVal) {
return 0;
} else {
return aVal < bVal ? -1 : 1;
}
});
for(var indice in stored) {
newTab.splice(indice, 0, stored[indice]);
}
console.log(newTab);
I am new to google api. I am able to do this file upload from app script and any file which is uploaded through that script get stored to my drive only. But how to do this using javascript. Example ...
I am new to google api. I am able to do this file upload from app script and any file which is uploaded through that script get stored to my drive only. But how to do this using javascript. Example ...
I have a large JSON file with a snippet of the file below. Please ignore the values as they are only used as placeholders for this question. "restaurants":[ "name": "Burger King", ...
I have a large JSON file with a snippet of the file below. Please ignore the values as they are only used as placeholders for this question. "restaurants":[ "name": "Burger King", ...
I have three sliding panels in my html. The thing is that panels overlay each other when open. I would like to get a function that create an event when one panel (eg. #panel1) is open already and the ...
I have three sliding panels in my html. The thing is that panels overlay each other when open. I would like to get a function that create an event when one panel (eg. #panel1) is open already and the ...
I have a Meteor method that I call from the client, which in turn updates all fields in a single document. In the last line of code below I try to immediately run the same update on the client to get ...
I have a Meteor method that I call from the client, which in turn updates all fields in a single document. In the last line of code below I try to immediately run the same update on the client to get ...