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'
},
....
]