E
E
Elizaveta Cezurk2021-04-12 12:45:06
JavaScript
Elizaveta Cezurk, 2021-04-12 12:45:06

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"

Each time the data is different, and different time unixTimestamp
I somehow need to sort them by time, but everything that I have not tried does not work:
function comp(a, b) {
  return new Date(a.my_array .date).getTime() - new Date(b.my_array .date).getTime();
}

I understand that the time is in unix, but I can not think of something that works, thanks in advance for your answers!
I split these arrays into JS+JQuery.
Here is the code:
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);

I receive json through WS connection.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2021-04-12
@Stalker_RED

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.

To compare these dates, it is enough to simply compare two numbers.
If, as you put it, "time in unix" is greater than some other, then it is later. If less, then sooner. And if you subtract one from the other, then you can even calculate the difference. Magic.

S
StiflerProger, 2021-04-12
@StiflerProger

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 question

Ask a Question

731 491 924 answers to any question