Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

javascript - How to sort only few elements in an array of object by date property

I have an array of objects like below.

var array = [{id: 1, date:'Mar 12 2012 10:00:00 AM', status:'done'}, 
                 {id: 2, date:'Mar 8 2012 08:00:00 AM', status:'done'},
                 {id: 3, date:'Mar 15 2012 10:00:00 AM', status:'done'}, 
                 {id: 4, date:'Mar 5 2012 08:00:00 AM', status:'done'},
                 {id: 5, date:'Mar 30 2012 10:00:00 AM', status:'done'}, 
                 {id: 6, date:'Mar 5 2012 08:00:00 AM', status:'done'},
                 {id: 7, date:'Mar 1 2012 10:00:00 AM', status:'done'}, 
                 {id: 8, date:'Mar 7 2012 08:00:00 AM', status:'notdone'},
                 {id: 9, date:'Mar 12 2012 10:00:00 AM', status:'notdone'}, 
                 {id: 10, date:'Mar 2 2012 10:00:00 AM', status:'notdone'}, 
                 {id: 11, date:'Mar 6 2012 10:00:00 AM', status:'notexist'}, 
                 {id: 12, date:'Mar 8 2012 08:00:00 AM', status:'notexist'}];

I am trying to sort first few elements in my array which has status:'done' and leave remaining records as it is.

Can someone help me with how I can put this condition and sort the elements.

Thanks.

question from:https://stackoverflow.com/questions/65948517/how-to-sort-only-few-elements-in-an-array-of-object-by-date-property

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

var array = [{id: 1, date:'Mar 12 2012 10:00:00 AM', status:'done'}, 
                 {id: 2, date:'Mar 8 2012 08:00:00 AM', status:'done'},
                 {id: 3, date:'Mar 15 2012 10:00:00 AM', status:'done'}, 
                 {id: 4, date:'Mar 5 2012 08:00:00 AM', status:'done'},
                 {id: 5, date:'Mar 30 2012 10:00:00 AM', status:'done'}, 
                 {id: 6, date:'Mar 5 2012 08:00:00 AM', status:'done'},
                 {id: 7, date:'Mar 1 2012 10:00:00 AM', status:'done'}, 
                 {id: 8, date:'Mar 7 2012 08:00:00 AM', status:'notdone'},
                 {id: 9, date:'Mar 12 2012 10:00:00 AM', status:'notdone'}, 
                 {id: 10, date:'Mar 2 2012 10:00:00 AM', status:'notdone'}, 
                 {id: 11, date:'Mar 6 2012 10:00:00 AM', status:'notexist'}, 
                 {id: 12, date:'Mar 8 2012 08:00:00 AM', status:'notexist'}];
                 
var doneItems = array.filter(r => r.status === 'done');
var remainingItems = array.filter(r => r.status !== 'done');

doneItems.sort((a, b) => (new Date(a.date) - new Date(b.date)));

console.log(doneItems.concat(remainingItems));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...