" 0:" FZ | Fly Dubai" 1:" LH | Lufthansa" 2:" IX | Air India Express " 3:" 6E |靛蓝
" 0:" IX | Air India Express" 1:" FZ | Fly Dubai" 2:" LH | Lufthansa " 3:" 6E |靛蓝
str = str.split('|');
str = str.sort(function (a,b) {
if (a === '0' || b === '0')
return (b === a) ? 0 : (a < b) ? 1 : -1;
return (a < b) ? -1 : (a === b) ? 0 : 1;
});
答案 0 :(得分:0)
let airlines = [
"FZ|Fly Dubai",
"LH|Lufthansa",
"IX|Air India Express",
"6E|IndiGo",
];
let sorted = airlines.sort(function(a, b) {
return a.split('|')[1].localeCompare(b.split('|')[1]);
});
console.log(sorted);
答案 1 :(得分:0)
不需要jQuery。
假设private
s = '"0:"FZ|Fly Dubai "1:"LH|Lufthansa "2:"IX|Air India Express "3:"6E|IndiGo'
要以初始格式恢复,您可以进行映射。即:
s.split(/\"[^\"]+\"/) // regex parses entries
.filter( // removes invalid entries
function(a){
return a.length > 0 && a.indexOf("|") > -1
}).sort( // sorts based on airline name
function(a,b){
return a.split("|")[1] > b.split("|")[1]
});