Arrange in chronological order and organize based on an external array

Presenting an Array of Objects with various transactions:

[{
  date: 2022-04-07T01:00:00.000+00:00,
  type: 'profit'
  amount: 200
},{
  date: 2022-04-07T01:00:00.000+00:00,
  type: 'withdraw'
  amount: 600
},{
  date: 2022-04-07T01:00:00.000+00:00,
  type: 'invest'
  amount: 900
},{
  date: 2022-04-08T01:00:00.000+00:00,
  type: 'deposit'
  amount: 200
},{
  date: 2022-04-08T01:00:00.000+00:00,
  type: 'profit'
  amount: 200
}]

The dates in the data source are unordered, so a sorting operation by date is necessary like this:

this.allTransactions.sort((a: any, b: any) => {
  return new Date(b.date).getTime() - new Date(a.date).getTime();
});

In addition to sorting by date, there is a requirement to sort objects with the same date according to an external array:

['invest', 'withdraw', 'profit']

A challenge arises in implementing the secondary sorting for objects within the same date group based on the order defined in the array mentioned. The array this.allTransactions is part of an *ngFor loop within Angular 12.

Answer №1

To enhance your current logic, consider adding an additional condition to handle cases where the initial one results in 0

const typeOrder = ['buy', 'sell', 'trade']

transactions.sort((x, y) => {
  return new Date(y.date).getTime() - new Date(x.date).getTime() || typeOrder.indexOf(x.type) - typeOrder.indexOf(y.type)
});

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

What could be the reason for the lack of controller updates despite changes made to the service

Could someone please help me solve the issue with my code? I expected that after clicking the button, the text would be updated. However, it seems to not be working as intended. Any assistance you can provide would be greatly appreciated. main.js x = a ...

Creating custom validation in Vuetify for password confirmation is essential in ensuring that

While developing a Vue.js template, I encountered a scenario on the sign-up page where I needed to compare passwords during user registration. To achieve this, I implemented a custom validation rule in the code snippet below: <v-text-field label=" ...

When an unrelated value is changed, the Vue 2 dynamic component experiences a loss of its values during the refresh

I've been grappling with this issue for quite some time now and I'm beginning to suspect it might be a bug. I'm currently utilizing a dynamic vue component to substitute markers in a body of text with input fields. The functionality seems t ...

Remove all input fields within an HTML file using a TypeScript method implemented in an Angular 2 component

Within my Angular project, there are several input elements in the HTML file that are not enclosed within a form tag. I am looking to create a function in the TypeScript file that will clear all of these inputs. I attempted to utilize ViewChild, but it a ...

Utilize TypeScript to access scope within a directive

Is there a way to access the controller's scope properties using my custom TypeScript directive? For example, in this snippet below, I am trying to retrieve and log scope.message: /// <reference path="typings/angularjs/angular.d.ts" ...

What is a practice for utilizing navCtrl.push() with a variable storing a class name?

Currently, I am utilizing Visual Studio Code for Ionic 3 development with AngularJS/Typescript. In my code, I am using this.navCtrl.push() to navigate to different pages within the application. Specifically, I have two classes/pages named "level1" and "lev ...

What significant alterations can be found in the architecture of Angular 2?

Hello, I am currently exploring Angular 2 and Stack Overflow. I am curious about the significant architectural differences between Angular 2 and its predecessor, Angular. In Angular, there were functions like $apply, $digest, $evalAsync, and others. Why we ...

Tips on arranging an array based on dates and data in JavaScript?

I have an array with dates and other data that I need to sort based on the dates. I'm unsure of how to go about this. Can someone please assist me? Here is the array in question: 0:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: ...

Issues with running Angular 2-4 SETUP on Windows operating system

I recently started working with Angular 2/4 and everything was running smoothly until I followed the instructions on https://github.com/angular/angular-cli. I have Angular 2-4 installed on my machine, but after a recent Windows update, I encountered issues ...

Solve the issue of the __typename union

Imagine having the following union: export type IBookmarkItemFragment = | ({ __typename: "Story" } & { story: number; }) | ({ __typename: "Product" } & { product: number; }) | ({ __typename: "Project" } & { proj ...

"Troubleshooting alert: Encounter an error message saying 'process is not defined' when attempting to load the grpc package using proto-loader and grpc-js within a

Looking for help with integrating a typescript Vue.js component that needs to make a grpc call. The proto file can be found here: https://github.com/floydjones1/ts-node-grpc/blob/main/proto/random.proto Here is the component.vue code snippet: <script ...

AngularJS binding variables to HTML tag attributes

Currently, I am going through the tutorials and documentation for Angularjs that is provided on the official Angularjs website. One of the examples involves adding a select box for ordering which looks like this: <select ng-model="orderProp"> ...

Automatically copy any chosen selection on the page to the clipboard

Is there a way to copy any selection from a webpage to the clipboard, regardless of where it is located on the page (div, text input, password input, span, etc.)? I have created a function that can retrieve the selected text, but I am struggling with sett ...

The Google Maps directions stay visible even when new routes are generated

Utilizing the Google Maps Javascript API V3 in my Android WebView has presented a new issue. When I request directions from point A to B, it displays correctly. However, when I switch the endpoints to go from A to C, the route from A to B does not disappea ...

Rendering issue with component

I am encountered with a situation where one component is failing to render its sub-component. Surprisingly, there are no visible errors in the console. The data I'm expecting from the web call is also coming through without any issues. However, for so ...

How to utilize a Jquery loop, implement a condition, and store the results in

After using dd() in PHP, the array displayed is as follows: 1 [▼0 => "1,18,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,19,20,21,22,23,24"] I need to iterate through the array and o ...

An unexpected exception arose during the npm audit fix process in Angular

While working on an Angular application with the ng new command, I encountered 7 vulnerabilities. Angular advised using npm audit fix to address these vulnerabilities. My question is, if I choose not to run npm audit fix, the app still functions and run ...

Iterate over an array utilizing the $.getJSON method for data retrieval

I have encountered an issue while using a for loop to iterate through an array of dates in a JSON request. The problem is that the loop seems to be fetching only the first item in the array each time it iterates, as if ignoring the variable i or being cach ...

I will not be accessing the function inside the .on("click") event handler

Can someone help me troubleshoot why my code is not entering the on click function as expected? What am I missing here? let allDivsOnTheRightPane = rightPane.contents().find(".x-panel-body-noheader > div"); //adjust height of expanded divs after addi ...

Converting JSON response from REST into a string format in Angular

I have developed a REST service in Angular that sends back JSON response. To perform pattern matching and value replacement, I require the response as a string. Currently, I am utilizing Angular 7 for this task. Below is an example of my service: getUIDa ...