0

const res = [
  {
    _id: '5fbfa729fc46a415ce5503a6',
    first_name: 'AAA',
    last_name: 'BBBB',
    story: [
      {
        _id: '6136096f4255d84bcb4a7144',
        timestamp: 1630931311227,
        expire_at: '2021-09-06T12:28:31.227Z'
      }
    ]
  },
  {
    _id: '5fbf6f91aff7f3320a906547',
    first_name: 'DDD',
    last_name: 'FFFF',
    story: [
      {
        _id: '613609414255d84bcb4a7122',
        timestamp: 1630931265409,
        expire_at: '2021-09-06T12:27:45.409Z'
      }
    ]
  },
  {
    _id: '5fbfa748fc46a415ce5503a8',
    first_name: 'EEEE',
    last_name: 'FFFF',
    story: [
      {
        _id: '613609184255d84bcb4a710a',
        timestamp: 1630931224383,
        expire_at: '2021-09-06T12:27:04.383Z'
      },
      {
        _id: '613709f49223350dfdaec618',
        timestamp: 1630996980379,
        expire_at: '2021-09-07T06:43:00.379Z'
      },
      {
        _id: '61372d81c0a7ec4fa0f7a8e3',
        timestamp: 1631006081890,
        expire_at: '2021-09-07T09:14:41.890Z'
      }
    ]
  }
];

res.forEach((val, key) => {
  console.log('val', val.story);
  val.story.sort((a, b) =>
    a.timestamp > b.timestamp ? 1 : b.timestamp > a.timestamp ? -1 : 0
  );
});

I want to sort the object based on story's timestamp. who has posted latest story it should come first for example EEE has latest story post it should come at first place.

this object should be at first place after that others which false later this story and so on

{
    _id: '5fbfa748fc46a415ce5503a8',
    first_name: 'EEEE',
    last_name: 'FFFF',
    story: [....]
  }
1
  • 1. Find out the most recent story in each object. 2. Copy its _id directly inside its parent object in a new key, for instance in most_recent_story. 3. Sort by most_recent_story. Commented Sep 7, 2021 at 9:55

3 Answers 3

1

const res = [ { _id: '5fbfa729fc46a415ce5503a6', first_name: 'AAA', last_name: 'BBBB', story: [ { _id: '6136096f4255d84bcb4a7144', timestamp: 1630931311227, expire_at: '2021-09-06T12:28:31.227Z' } ] }, { _id: '5fbf6f91aff7f3320a906547', first_name: 'DDD', last_name: 'FFFF', story: [ { _id: '613609414255d84bcb4a7122', timestamp: 1630931265409, expire_at: '2021-09-06T12:27:45.409Z' } ] }, { _id: '5fbfa748fc46a415ce5503a8', first_name: 'EEEE', last_name: 'FFFF', story: [ { _id: '613609184255d84bcb4a710a', timestamp: 1630931224383, expire_at: '2021-09-06T12:27:04.383Z' }, { _id: '613709f49223350dfdaec618', timestamp: 1630996980379, expire_at: '2021-09-07T06:43:00.379Z' }, { _id: '61372d81c0a7ec4fa0f7a8e3', timestamp: 1631006081890, expire_at: '2021-09-07T09:14:41.890Z' } ] } ];

function getEarliest(obj) {
    return obj.reduce((latest, e) => Math.max(latest, e.timestamp), 0)
}
const sorted = res.sort((a,b) => {
    return getEarliest(b.story) - getEarliest(a.story)
})
console.log(sorted)
this should work

Sign up to request clarification or add additional context in comments.

Comments

0

const res = [
  {
    _id: '5fbfa729fc46a415ce5503a6',
    first_name: 'AAA',
    last_name: 'BBBB',
    story: [
      {
        _id: '6136096f4255d84bcb4a7144',
        timestamp: 1630931311227,
        expire_at: '2021-09-06T12:28:31.227Z'
      }
    ]
  },
  {
    _id: '5fbf6f91aff7f3320a906547',
    first_name: 'DDD',
    last_name: 'FFFF',
    story: [
      {
        _id: '613609414255d84bcb4a7122',
        timestamp: 1630931265409,
        expire_at: '2021-09-06T12:27:45.409Z'
      }
    ]
  },
  {
    _id: '5fbfa748fc46a415ce5503a8',
    first_name: 'EEEE',
    last_name: 'FFFF',
    story: [
      {
        _id: '613609184255d84bcb4a710a',
        timestamp: 1630931224383,
        expire_at: '2021-09-06T12:27:04.383Z'
      },
      {
        _id: '613709f49223350dfdaec618',
        timestamp: 1630996980379,
        expire_at: '2021-09-07T06:43:00.379Z'
      },
      {
        _id: '61372d81c0a7ec4fa0f7a8e3',
        timestamp: 1631006081890,
        expire_at: '2021-09-07T09:14:41.890Z'
      }
    ]
  }
];

let getRecent = (story) => Math.max(...story.map(s => s.timestamp))
  
  res.sort((a, b) =>{
as =getRecent(a.story);
bs =getRecent(b.story);
   return as > bs ? 1 : bs > as ? -1 : 0
});

console.log(res)

Comments

0

First need to sort the nested story array, sort by big timestamp. Then sort the first layer array by comparing the timestamp of first element of each object

    res.forEach(function (element) {
        element.story.sort(function (a, b) {
            return (b.timestamp - a.timestamp)
        })
    })
    res.sort(function (a, b) {
        return (b.story[0].timestamp - a.story[0].timestamp)
    })
    console.log(res);

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.