Arrange items in an array by two criteria

Sorting elements in a JavaScript array can be tricky, especially when trying to order them based on specific criteria like Ptr and Outputs values. In this case, the desired ordering is Node 0 -> Node 2 -> Node 1, where the Output of an element matches the Ptr of the next element. If you're new to JS, figuring out how to achieve this may seem daunting.

nodeObjs = [{
  Node: '0',
  Id: '8057358647298',
  Ptr: '2454369535744',
  Outputs: '2454369536096',
  Type: 'SrcStream',
  NodeDetails: 'MediaType=[FrameSize=8246337209332 (1920,1012),BitRate=3733169,Compressed=1,MajorType={73646976-0000-0010-8000-00AA00389B71},Stride=0,Rotation=0,FrameRate=42949672960397843 (10000000,397843),PixelAspectRatio=4294967297 (1,1),InterlacedMode=7,Subtype={34363248-0000-0010-8000-00AA00389B71}]'
},
{
  Node: '1',
  Id: '8057358647297',
  Ptr: '2454369537680',
  Outputs: '',
  Type: 'Out',
  NodeDetails: 'MediaType=[FrameSize=8246337209332 (1920,1012),BitRate=3733169,Compressed=0,MajorType={73646976-0000-0010-8000-00AA00389B71},Stride=1920,Rotation=0,FrameRate=42949672960397843 (10000000,397843),PixelAspectRatio=4294967297 (1,1),InterlacedMode=7,Subtype={3231564E-0000-0010-8000-00AA00389B71}]'
},
{
  Node: '2',
  Id: '8057358647303',
  Ptr: '2454369536096',
  Outputs: '2454369537680',
  Type: 'Trnsfrm',
  NodeDetails: 'CLSID={62CE7E72-4C71-4D20-B15D-452831A87D9D},HWMFT=0,InputType=[FrameSize=8246337209332 (1920,1012),BitRate=3733169,Compressed=1,MajorType={73646976-0000-0010-8000-00AA00389B71},Stride=0,Rotation=0,FrameRate=42949672960397843 (10000000,397843),PixelAspectRatio=4294967297 (1,1),InterlacedMode=7,Subtype={34363248-0000-0010-8000-00AA00389B71}],OutputType=[FrameSize=8246337209332 (1920,1012),BitRate=3733169,Compressed=0,MajorType={73646976-0000-0010-8000-00AA00389B71},Stride=1920,Rotation=0,FrameRate=42949672960397843 (10000000,397843),PixelAspectRatio=4294967297 (1,1),InterlacedMode=7,Subtype={3231564E-0000-0010-8000-00AA00389B71}]'
}]

Answer №1

Here is an example of ES6 syntax:

nodeObjs.sort((a, b) => {
    return a.Outputs.localeCompare(b.Ptr) && b.Node - a.Node;
}).reverse()

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

connecting elements in an object's array to another array within a nested object

Is there a way to iterate through each string value in the imageKeys array and use it to create a URL for each object in the images array within the nested ImageList object, all without changing the original object? const myInitialStateFromParent = { ...

Scrollbar auto-scrolling problem

I have been attempting to create a div that displays content scrolling horizontally, similar to a slideshow, after a set period of time. I am utilizing the mcustomscrollbar plugin in my implementation. As I iterate through a for loop to increment the IDs a ...

When scrolling, use the .scrollTop() function which includes a conditional statement that

As a newcomer to jQuery, I've been making progress but have hit a roadblock with this code: $(window).scroll(function(){ var $header = $('#header'); var $st = $(this).scrollTop(); console.log($st); if ($st < 250) { ...

Oops! Next JS encountered an unhandled runtime error while trying to render the route. The

I keep receiving the error message Unhandled Runtime Error Error: Cancel rendering route Within my navBar, I have implemented the following function: const userData={ id:1, email: "", name: "", lastName: "", ...

Manipulating Data in TypeScript: Creating a Mutated Copy of a List of Dictionaries

After going through multiple answers, it appears that there might be a logical error. However, I am struggling to find a solution for this issue. In TypeScript/JavaScript, I have two lists of dictionaries. One list is a copy of the other for tracking purp ...

In this context, 'number' is typically a data type, but it seems to be functioning as a specific value

Greetings! I'm encountering an issue with this Angular 4 code. Whenever I attempt to type a number, I encounter the following error: 'number' only refers to a type, but is being used as a value here.ts(2693). View image description here ...

Difficulty encountered in generating an array of JSON entities

So, I am tasked with creating an array of JSON objects in a specific format: [object, object, object...] To achieve this, I iterate through all the selected rows in my jQuery jtable, consolidating all parameters for each row into a single object. After r ...

Setting compilerOptions.isCustomElement for VueJS 3 in a Laravel project: A step-by-step guide

I am currently working on integrating VueJS 3 into a Laravel project and utilizing a JS file to implement a markdown toolbar. The JS file contains functions that generate buttons for applying various markdown options. Everything is functioning properly, bu ...

Adding numerous lines into an array and then evaluating the array in a bash script

Seeking help with transforming an array (file c) using Bash 4.1 or Python/numpy as I am new to it and struggling to fully grasp the concept. Here's my issue: Given array: 1 E 2 A 3 Z 5 R 6 C 8 A I want to insert a line when there is an increment gr ...

Obtaining data attributes in Angular 8

I'm working with Angular 8 and I came across an issue. In my code snippet, there are two data attributes assigned to a button element, but only one attribute is showing up. Is this a syntax error or a bug? <button [attr.data-popolamento]="all" [a ...

The issue I'm facing is that the ng-click functionality in Angular JS is not functioning properly when the

Upon loading the page, my JavaScript function creates a tree structure from JSON data and generates a list of anchor tags which look like this: <a ng-click="shareParent(4619)">Some data</a> Despite associating the ng-click directive with the ...

Automatically select a value in MUI AutoComplete and retrieve the corresponding object

I recently set up a list using the MUI (v4) Select component. I've received a feature request to make this list searchable due to its extensive length. Unfortunately, it appears that the only option within MUI library for this functionality is the Au ...

Conversion Error: Attempting to switch from void to float[] in Color.RGBToHSV

Utilizing the ADT library import android.graphics.Color; Encountering a problem with Type mismatch: cannot convert from void to float[]. float[] hsv = new float[3]; hsv = Color.RGBToHSV(rgb[0], rgb[1], rgb[2], hsv); An error occurs at Color.RGBToHSV(r ...

Challenge with executing javascript library (photo sphere viewer)

I was excited to incorporate Photo Sphere Viewer into my project. After running npm i photo-sphere-viewer I noticed that the modules were successfully downloaded. Following that, I added this line inside my project: import PhotoSphereViewer from ' ...

Error: Unable to locate module with associated type definitions when utilizing Typescript in Next.js

Currently, I am working on a next.js project that I'm attempting to integrate typescript into. The structure of my folders is organized as follows: api aggregation.ts interfaces index.ts components Component1 index.js index.module.css ...

How can I design a versatile button in HTML/CSS3/jQuery/Javascript that utilizes images for borders and corners for maximum scalability?

Is there a way to create a completely scalable button in HTML/CSS3/jQuery+Plugins using images for borders and corners? I have attempted a messy method, but I am confident that there are more effective solutions available. The approach I considered invol ...

Retrieve Object3D in three.js based on a custom property value

In my coding project, I have been creating instances of LineSegments in the following manner: const geometry = new THREE.BufferGeometry(); geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); const edges = new THREE.EdgesGe ...

Storing an Array in JSON Format to a File with PHP

{"Files": [ {"file_name": "Text_file.txt","path": "to_folder","file_id": "abc12345"}, {"file_name": "Img.jpg","path": "to_folder" ...

Troubleshoot: Unable to send or receive messages in Socket.IO Chat

I recently tried following a tutorial on socket.io chat, which can be found here. Although the tutorial video showed everything working perfectly, I have encountered issues with my implementation. It seems like the messages are not being sent or received ...

Why are traditional Angular dependencies still necessary even when typedefinitions are being used?

I am currently in the process of transitioning my Angular 1.5 project to use TypeScript. The project is compiled with webpack and even though I have included Angular type definitions in my package.json file as shown below, "@types/angular": "~1.5.19", "@t ...