简体   繁体   中英

Node Js sorting Json object based on the unix timestamp

I have the following json object:

var my_array = [
  {
    timestamp: '1571967208',
    transaction: 'DEPOSIT',
    name: 'Bob',
    amout: '10'
  },
  {
    timestamp: '1571967200',
    transaction: 'DEPOSIT',
    token: 'Roy',
    amount: '60'
  },
  {
    timestamp: '1571967189',
    transaction: 'WITHDRAWAL',
    token: 'Bob',
    amount: '10'
  },
  {
    timestamp: '1571966982',
    transaction: 'WITHDRAWAL',
    token: 'bob',
    amount: '50'
  }
]

I'm trying to sort this by timestamp (latest to last) and display new array but the timestamp in a human-readable format like 'Friday, October 25, 2019, 1:33:28 AM'.

I used the following code in my program:

function comp(a, b) {
  return new Date(a.my_array .date).getTime() - new Date(b.my_array .date).getTime();
}

result.sort(comp);

But it's not sorting is it wrong or if any other way to do this? For example new array like below:

var my_array = [
  {
    timestamp: 'Friday, October 25, 2019 1:33:28 AM',
    transaction: 'DEPOSIT',
    name: 'Bob',
    amout: '10'
  },
  ....
]

There are three problems with that code:

  1. You're using the date property, but the example uses the name timestamp .

  2. The timestamp values are strings. The Date constructor looks at the type of the argument you pass it and does different things for strings and numbers. You want to convert those to number first (and multiply by 1000, to convert from seconds to milliseconds). (But there's no need for Date if you're just comparing them.)

  3. You're using a.my_array.date and such in the callback, but what you want to use are the callbacks arguments directly.

Since - will implicitly convert to number, the sorting part is:

function comp(a, b) {
    return a.timetstamp - b.timestamp;
}

Then use map on the result to format the timestamp as a string (or just forEach or any other form of loop), converting it to date like this:

var dt = new Date(entry.timestamp * 1000);

It's * 1000 because yoru timestamps are in seconds, but new Date expects milliseconds. * will also implicitly convert to number.

Then convert timestamp to string however you like.

Here's an example using toString :

my_array = my_array.sort(comp);
my_array.forEach(function(entry) {
    entry.timestamp = new Date(entry.timestamp * 1000).toString();
});

Live Example:

 var my_array = [ { timestamp: '1571967208', transaction: 'DEPOSIT', name: 'Bob', amout: '10' }, { timestamp: '1571967200', transaction: 'DEPOSIT', token: 'Roy', amount: '60' }, { timestamp: '1571967189', transaction: 'WITHDRAWAL', token: 'Bob', amount: '10' }, { timestamp: '1571966982', transaction: 'WITHDRAWAL', token: 'bob', amount: '50' } ]; function comp(a, b) { return a.timetstamp - b.timestamp; } my_array = my_array.sort(comp); my_array.forEach(function(entry) { entry.timestamp = new Date(entry.timestamp * 1000).toString(); }); console.log(my_array);

Obviously, you'll have to adjust the string format to your desired one. The only built-in formats are toString and toISOString , otherwise you have to roll your own or use a library like Moment.

You may try this

result.sort(function(a,b){
  return ((new Date(a.timestamp).getTime()) - (new Date(b.timestamp).getTime()))
})

May be this is helpful for you.

 var my_array = [ { timestamp: '1571967208', transaction: 'DEPOSIT', name: 'Bob', amout: '10' }, { timestamp: '1571967200', transaction: 'DEPOSIT', token: 'Roy', amount: '60' }, { timestamp: '1571967189', transaction: 'WITHDRAWAL', token: 'Bob', amount: '10' }, { timestamp: '1571966982', transaction: 'WITHDRAWAL', token: 'bob', amount: '50' } ] function comp(a, b) { return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(); } let newarr = my_array.sort(comp); let x = newarr.map((data)=>{ let p = data; p.timestamp = new Date(parseInt(data.timestamp*1000)).toString(); return p; }); console.log(x) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM