Answer the question
In order to leave comments, you need to log in
How to sort json array by date?
Good afternoon, I receive such arrays:
blocks: Array(1)
0:
blockHeight: 58
transactions: Array(1)
0:
amount: 947.7280106740033
currency: "BTC"
fee: 0.1
recipientAddressRank: {address: "user#4", currency: "BTC", suspicion: 0.7374174}
senderAddressRank: {address: "user#1", currency: "BTC", suspicion: 0.073294}
transactionHash: "hash"
unixTimestamp: "1618219971"
function comp(a, b) {
return new Date(a.my_array .date).getTime() - new Date(b.my_array .date).getTime();
}
var data = JSON.parse(event.data);
console.log(data);
var blockslenght = data.blocks.length;
for (var i = blockslenght-1 ; i >= 0; i--) {
var transactionslenght = data.blocks[i].transactions.length;
for (var t = transactionslenght-1; t >= 0; t--) {
incomingOut += '<tr>';
incomingOut += '<td width="50">';
incomingOut += '<img src="static/image/'+data.blocks[i].transactions[t].currency+'.png">';
incomingOut += '</td>';
incomingOut += '<td width="75" class="currency">';
incomingOut += '<p>'+data.blocks[i].transactions[t].currency+'</p>';
incomingOut += '</td>';
incomingOut += '<td width="375">';
incomingOut += '<p class="hash">'+data.blocks[i].transactions[t].transactionHash;
incomingOut += '<span class="date">'+convertTimestamp(data.blocks[i].transactions[t].unixTimestamp)+'</span></p>';
incomingOut += '</td>';
incomingOut += '<td width="250">';
incomingOut += '<p class="amount">'+data.blocks[i].transactions[t].amount + ' ' + data.blocks[i].transactions[t].currency + '</p>';
incomingOut += '<span class="circle" style="background:'+color+'" title="'+data.blocks[i].transactions[t].suspicion+'"></span>';
incomingOut += '</td>';
incomingOut += '<td>';
incomingOut += '<a href="#" class="detailed"></a>';
incomingOut += '</td>';
incomingOut += '</tr>';
}
}
$('.incoming .content table').html(incomingOut);
Answer the question
In order to leave comments, you need to log in
Unix time (English Unix time, also POSIX time) is a system for describing moments in time, adopted by Unix and other POSIX-compatible operating systems. Defined as the number of seconds since midnight (00:00:00 UTC) on January 1, 1970 (Thursday); this moment is called the Unix Epoch.
const data = JSON.parse(event.data);
for (let block of data.blocks) {
// перебираем блоки
// сортируем транзакции по дате
// замени "a" с "b" если нужна сортировка в другую сторону
block.transactions.sort((a, b) => Number(a.unixTimestamp) - Number(b.unixTimestamp))
for (let transaction of block.transactions) {
incomingOut += '<tr>';
incomingOut += '<td width="50">';
incomingOut += '<img src="static/image/' + transaction.currency + '.png">';
// и так далее
}
}
$('.incoming .content table').html(incomingOut);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question