对data的time字段,按照由小到大排序;如果为空,则按原来的顺序放在后面。
var data=[{name:'xixi',time:'2019-09-03'},
{name:'haha',time:'2019-09-02'},
{name:'hehe',time:'2019-09-01'},
{name:'hihi',time:'2019-09-04'},
{name:'mumu',time:''},
{name:'dudu',time:''}
]
data.sort(function(a,b){
return process(a.time)-process(b.time)
})
function process(x){
return x.split('-').join('')
}
for(var i=0;i<data.length;i++){
if(data[i].time==''){
data.push(data.shift())
}
}
console.log(data)
// [ { name: 'hehe', time: '2019-09-01' },
// { name: 'haha', time: '2019-09-02' },
// { name: 'xixi', time: '2019-09-03' },
// { name: 'hihi', time: '2019-09-04' },
// { name: 'mumu', time: '' },
// { name: 'dudu', time: '' } ]