Sorting an array of objects by epoch time in JavaScript

Looking to organize the data fetched from the API based on time? Take a peek at one of the objects in the array below:

{
    "orderId": "xxxxxxxxxxxxx",
    "amount": 10,
    "status": "CREATED",
    "transactionTime": 1651498914,
    "type": "ADD",
    "mode": "UPI",
    "bankAccountId": {
        "bankAccountNumber": "xxxxxxxxxxxxx",
        "ifsc": "XXXX XXXX XXXX"
    }
}

Want to sort the array in descending order by transactionTime, which is in epoch format?

Here's an initial approach:

data.map((item) => item.transactionTime)
   .sort()
   .reverse()
   .map((item) => {
        return data.find((i) => i.transactionTime == item);
})

Although the current code works, it might not be the most efficient method. Consider exploring a more optimized solution, like using a priority queue as suggested by some.

Answer №1

One way to sort the data is by directly subtracting the times in the `sort` callback function.

data.sort((a, b)=>b.transactionTime - a.transactionTime);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Greetings: Obtaining an array of text within the <td> tags

Here is the HTML Source: <td bgcolor="#ffffbb" colspan=2><font face="Verdana" size=1>2644-3/4<br>QPSK<br><font color="darkgreen">&nbsp;&nbsp;301</font> - 4864</td> I am looking to extract text array wit ...

Tips for passing a JavaScript array to an MVC controller

In my asp.net application, I have implemented a functionality where users can order food. Additionally, there is an admin page where admins can login and create a menu for a specific week. I have successfully developed the "Menu maker" feature where all me ...

What could be causing my prompt for a username to not work properly as I attempt to develop a system that generates a unique code using the user's initials?

I'm currently working on a script that will be used by the secretaries in our school district to generate unique codes for new students or hires. The code will consist of an 8-digit output, a randomly generated 6-digit number, and the user's init ...

Image failed to load

Issue Encountered in Browser Console: https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404 While attempting to display the image on the browser, I am uncertain if the problem lies within my code or with food2fork. Code from index.js: // Alway ...

What is the best way to retrieve the total number of options within a dynamically generated <select> element using JavaScript in JSP?

To generate a page, I use the following code: <%List<String> someList = new ArrayList<String>(); someList = SQL();%> <select id=Select> <% for (int i =0; i < someList.size(); i++) { %> <option value=<%= someLis ...

Steps for refreshing Google reCAPTCHA on an AJAX-enabled webpage

I am encountering an issue with two captchas on my website. One captcha is loaded upon refresh, while the second captcha is loaded on a different page via ajax. The problem arises when I click on the "No" button after selecting it on the second page. I wan ...

Creating an overlay with CSS hover on a separate element and the ::before pseudo class

Issue: I am struggling to display the overlay when hovering over the circle element, rather than just the image itself. I have attempted to achieve this using CSS, but can't seem to make it work as intended. Any guidance and examples using JavaScript ...

The OnTextChanged Event of ASP.NET TextBox fails to trigger

I am currently working on implementing a search functionality on the website where a request is sent to the server every time the user enters text, letter by letter. I've attempted to use the OnTextChanged event, but unfortunately, it does not seem to ...

pause in execution between iterations of a for loop

Challenge I'm currently working on a function that processes a list of strings by printing each one on a new line, with CSS animations that gradually display the string over a few seconds. However, I'm struggling to make sure that the next strin ...

Sending a dynamically allocated number of objects by reference to a class

I am currently using a library to load images, which generates class objects from the files and includes drawing functions. I am in the process of creating a class that can manage these objects and execute some basic logic after loading my files. What woul ...

Preventing Body Overflow Without Affecting Iframe Overflow in Meteor.js

Currently, I am working on a project using Meteor for a Point of Sale (POS) system. For those unfamiliar with it, Meteor is a framework that allows for the use of JavaScript, jQuery, and various other web app scripting languages. The goal of this applicati ...

Spacing issues while utilizing the <textarea> element

<tr> <td> <b>Escalation: </td></b> <td> <TextArea name='escalation' onKeyDown=\"limitText(this.form.escalation,this.form.countdown,100);\" onKeyUp=\"limitText ...

Troubleshooting resizing images in React using Material UI's useStyles

When attempting to resize an image using React, I am encountering a problem where adjusting the height and width of the tag with useStyles does not reduce the size of the image but instead moves it around on the page. Here is the code snippet: import { ma ...

Calculating the number of rows using JQuery

After browsing similar questions on Stack Overflow, I have not found a solution that fits my specific case. I have multiple tables on a page that share the same template without unique IDs. I am trying to determine if the rows are empty when certain data i ...

Retrieve information from D3 line interpolation functions

I'm currently working on creating a D3 graph that features a shaded area in the middle. The top part of the shaded region aligns with the line graph. For reference, the data structure I'm using is as follows: line = [ {x: 0, y:1}, {x: 1, y: ...

anchor text substitution for hyperlink

I currently have a code snippet that replaces a span class with a hyperlink. The hyperlink contains an abbreviation, and the alternate text for the link also includes the same abbreviation. Now, I am looking to replace the second abbreviation in the alte ...

Trouble with Bootstrap v4 toggle drop-down menu functionality detected in local environment, yet functions correctly when live on the

Today, I've encountered an issue with my Bootstrap v4 collapsible hamburger menu on my local XAMPP server. Interestingly, the menu works perfectly fine on my public website when the display is 768px wide in Chrome and Firefox. I have searched through ...

Angular does not allow the transfer of property values from one to another

As of late, I have delved into learning Angular but encountered an issue today. I have the following Component: import { Component, OnInit } from '@angular/core'; import { SharedService } from '../home/shared.service'; import { IData } ...

Error: Unable to instantiate a THREE.Scene object in Three.js due to TypeError

Recently, I decided to experiment with Three.js in order to incorporate it into a project of mine. However, upon running the initial example provided in the documentation: <html> <head> <title>My first Three.js app</title& ...

What could be causing the lack of updates to my component in this todo list?

Based on my understanding, invoking an action in MobX should trigger a rerender for the observer. However, when I call the handleSubmit method in my AddTask component, it doesn't cause the TaskList observer to rerender. Should I also wrap AddTask in a ...