Arranging a multidimensional array using Ionic sorting techniques

I'm trying to sort a 2D array based on a specific value, here's the array in question:

[
  [
    {
      "Category": "Food",
      "Label": "Trsttzp",
      "Price": "45",
      "Date": "01/12/2018"
    }
  ],
  [
    {
      "Category": "Food",
      "Label": "Trst",
      "Price": "65",
      "Date": "01/13/2018"
    }
  ],
  [
    {
      "Category": "Food",
      "Label": "Ts",
      "Price": "99",
      "Date": "01/02/2018"
    }
  ],
  [
    {
      "Category": "Food",
      "Label": "Ts",
      "Price": "99",
      "Date": "01/12/2018"
    }
  ],
  [
    {
      "Category": "Food",
      "Label": "Haa",
      "Price": "55",
      "Date": "01/12/2018"
    }
  ],
  [
    {
      "Category": "Food",
      "Label": "qsd",
      "Price": "6",
      "Date": "01/12/2018"
    }
  ]
]

I attempted to use the "fast-sort" library from npm to sort it by Date, but it doesn't seem to be working. It seems like the issue lies in the fact that this library doesn't support 2D arrays. I'm having trouble finding a solution using TypeScript or another library that supports 2D arrays. Help!

Answer №1

It's actually quite easy to achieve this without relying on an external library.

const sortedArray = array.sort((item1, item2) => new Date(item1[0].Date) > new Date(item2[0].Date));

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

Unusual quirks in javascript variables when used with arrays

Unsure if this question has been asked previously. Nevertheless, I couldn't find it anywhere. I've observed a peculiar behavior that seems to occur only with arrays. Here is the typical behavior I anticipate from variables: var k = 10, m = ...

What is the connection between Strict Aliasing and the implementation of parallel arrays in a union?

Consider the following union: union { uint16_t halfwords[32]; uint32_t fullwords[16]; } my_union; What does the Strict Aliasing rule state with regards to: my_union.fullwords[0] = 1; printf("%d", my_union.halfwords[1]); Is my_union.halfwords[1] con ...

Neostrada's API can be used to iterate through the DNS results

I'm struggling to figure out how to loop through this response in a way that displays all the individual records rather than the entire "result" as one block of data. Here is my current result: {"results":[{"id":83213964,"domainId":648668,"name":"pc ...

Uncover the value type of a key in TypeScript without using a discriminated union

I want to implement a type map that ensures the type of an object's value for a specific key is determined by the value of another key. For example: type CurrencyValue = { code: string; value: number; }; type FieldMap = { text: string; curren ...

Organize array elements based on their values - using javascript

I am currently exploring the most effective approach to divide an array into multiple arrays based on specific operations performed on the values within the array. Imagine I have an array like this: var arr = [100, 200, 300, 500, 600, 700, 1000, 1100, 12 ...

Alert: Prop type validation error: The `component` prop provided to `ForwardRef(Link)` is invalid

We are facing an issue with our NextJS app where it works fine in production, and locally it renders correctly. However, we are encountering some unsightly warnings that we are trying to suppress. client.js:1 Warning: Failed prop type: Invalid prop `compon ...

Retrieve data from a JSON object within an HTML document

How do I display only the value 100 in the following div? <div> {{uploadProgress | async | json}} </div> The current displayed value is: [ { "filename": "Mailman-Linux.jpg", "progress": 100 } ] Here is my .ts file interface: interface IU ...

Even though there is data stored in the array, the React Native array.length appears to be returning a value

I am struggling with what appears to be a simple issue, and it's frustrating that I've had to seek help for this. The problem lies in iterating through an array messages: Message[] = [...]. No matter what method of iteration I try, it doesn&apos ...

How to troubleshoot the issue of "Error: (SystemJS) module is not defined" in Angular 2?

I am a beginner in the world of Angular2. It is known that in Angular2, there is a way to reference a file using a relative path by defining moduleId : module.id in the component meta data. However, I have tried doing it this way and keep encountering the ...

Tips for correctly setting object initial values in React CreateContext

How can I correctly define the initial value of the constance trainsDetails in React Create Context? The trainsDetails is an object with various properties, fetched as a single object from an endpoint and has the values specified below in the TrainsDetails ...

Is it possible to associate a generic TypeScript type with another type by utilizing its generic parameters?

I am faced with a situation where I have a family of APIs consisting of various related generic types (along with associated constraints). To illustrate this, I have crafted an example using familiar types as placeholders for better understanding. As I wo ...

What is the best method for exporting and importing types in React and Next.js apps?

Is there a way to export and import types from API responses in TypeScript? I have a type called Post that I want to re-use in my project. // pages/users.tsx type Post = { id: number; name: string; username: string; email: string; address: { ...

Looking to modify the height and width of an image when it is hovered over using inline CSS

My current project involves working with a dynamic template where the HTML code is generated from the back-end using TypeScript. I am trying to implement inline CSS on hover, but despite having written the necessary code, it does not seem to work as intend ...

The value of req.headers('Authorization') has not been defined

I'm experiencing difficulty with my code as the token is coming back as undefined. Here is the frontend section: export const fetchUser = async (token: any) => { const res = await axios.post('/user/getuser', { headers ...

An issue occurred while trying to execute the `ionic serve` command, and the result of

Facing an issue with the ionic serve command - I keep getting the error message "Hmm, we can't reach this page" and when attempting to view the result of the ionic address command, there is no output. Any assistance in resolving this would be greatly ...

Do Angular FormControl objects have the capability to accept input values of various types, or are they limited to TypeScript primitive types?

When creating a reactive form in Angular using FormControl objects in a FormGroup, I encountered an issue. While passing primitive arguments as values for an HTML input select control works fine, when passing an object of a self-defined class, the value in ...

react Concealing the Card upon clicking a different location

Utilizing a combination of React and TypeScript, this component allows for the card to be toggled between shown and hidden states upon clicking on the specified div tag. However, there is a need to ensure that the card is hidden even if another area outs ...

Arranging elements within a structured array using the bubble sort algorithm in C++

I am attempting to arrange my Student data type array, named student_database, by its member ID array from lowest to highest using a bubble sort algorithm. I found an example in a C++ textbook and implemented it into my program. Although the code compile ...

The attribute 'XXX' is not found within the type 'IntrinsicAttributes & RefAttributes<any>'

My coding hobby currently involves working on a React website using TypeScript. I recently came across a free Material UI template and decided to integrate it into my existing codebase. The only challenge is that the template was written in JavaScript, cau ...