0

I want to sort an array by date. But ignore the first item in the array. Any help would be much appreciated.

I have this currently:

articles.sort(function(a,b){
    return new Date(b.published) - new Date(a.published);
});

my array looks like this:

[
    {id: 1, published: Mar 12 2012 08:00:00 AM}, 
    {id: 2, published: Mar 9 2012 08:00:00 AM},
    {id: 3, published: Mar 15 2012 08:00:00 AM},
    {id: 4, published: Mar 22 2012 08:00:00 AM},
    {id: 5, published: Mar 8 2012 08:00:00 AM}
];

I just need to sort everything by date from id 2 - 5

What I have sorts everything.

Thanks

3
  • Your function relies on a published property that doesn't exist in the array you provided. Please update your post as necessary. Commented Jun 18, 2019 at 21:20
  • Yes thank I've amended that. Commented Jun 18, 2019 at 21:22
  • you want to ignore the first element in the array after sorting or before sorting Commented Jun 18, 2019 at 21:22

4 Answers 4

1

Use the shift() to remove the first element and unshift() to put it back at the first position:

var first = articles.shift();
articles.sort(function(a,b){
    return new Date(b.published) - new Date(a.published);
});
articles.unshift(first);
Sign up to request clarification or add additional context in comments.

Comments

1

Then, you can shift the first row, and readd after sorting.

var articles = [
    {id: 1, published: "2018-01-09"}, 
    {id: 2, published: "2019-01-01"},
    {id: 3, published: "2019-01-04"},
    {id: 4, published: "2019-01-03"},
    {id: 5, published: "2019-01-02"}
];
var first = articles.shift();
articles.sort((a, b) => {
    return new Date(b.published) - new Date(a.published);
});
articles.unshift(first);
console.log(articles);

1 Comment

You've beat me to it. I am typing to slow =)
1

You can simply exclude the id:1 from your Array.sort by returning 1 if the id matches 1:

let dates = [ {id: 1, published: 'Mar 12 2012 08:00:00 AM'}, {id: 2, published: 'Mar 9 2012 08:00:00 AM'}, {id: 3, published: 'Mar 15 2012 08:00:00 AM'}, {id: 4, published: 'Mar 22 2012 08:00:00 AM'}, {id: 5, published: 'Mar 8 2012 08:00:00 AM'} ];

let result = dates.sort((a,b) => 
   a.id == 1 || b.id == 1 ? 1 : new Date(a.published) - new Date(b.published))

console.log(result)

This way you would not need to concat, slice or shift anything.

Comments

0

To ignore the first item in the array, sort on the sliced array, and then merge with the first item. Also note that the timestamp needs to be a string - currently its invalid.

const articles = [{id:1,published:"Mar 12 2012 08:00:0 AM"},{id:2,published:"Mar 9 2012 08:00:0 AM"},{id:3,published:"Mar 15 2012 08:00:0 AM"},{id:4,published:"Mar 22 2012 08:00:0 AM"},{id:5,published:"Mar 8 2012 08:00:0 AM"}];
const res = [].concat(articles[0], articles.slice(1).sort(({ published: a }, { published: b }) => new Date(a) - new Date(b)));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.