Sort elements in JavaScript without using lambda function by using a sorting function

When using a lambda function, the solution worked well for me. However, due to some failed karma test cases, I had to refrain from using lambda functions in sorting. I am currently facing issues on how to tackle this problem, as the code below is not producing the desired results.

var test=[{ID: "91",Name: "sgtue", standardID: "1"},
{ID: "41",Name: "asdfasdf", standardID: "2"},
{ID: "5", Name: "credd", standardID: "2"},
{ID: "2",Name: "dddawer", standardID: "2"},
{ID: "2",Name: "dsfadf", standardID: "3"},
{ID: "275", Name: "xcvcvc", standardID: "201"}
];


var groupOrder = [1,3,2,201];
var testSorted = test.sort(function (a, b) {groupOrder.indexOf(parseInt(a.standardID))-groupOrder.indexOf(parseInt(b.standardID))}); 


console.log(testSorted);

The same logic can be viewed at the following link: Live Example

In addition, you can find my related question on Stack Overflow here: How to customize grouping in AngularJS from array of objects

Answer №1

To obtain the outcome of a sorted calculation, simply return the result.

var test = [{ ID: "91", Name: "sgtue", standardID: "1" }, { ID: "41", Name: "asdfasdf", standardID: "2" }, { ID: "5", Name: "credd", standardID: "2" }, { ID: "2", Name: "dddawer", standardID: "2" }, { ID: "2", Name: "dsfadf", standardID: "3" }, { ID: "275", Name: "xcvcvc", standardID: "201" }],
    groupOrder = [1, 3, 2, 201];

test.sort(function(a, b) {
    return groupOrder.indexOf(+a.standardID) - groupOrder.indexOf(+b.standardID);
    // ^^^
});

console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

How can you locate the position of a selector within a parent or set using jQuery

Could someone please help me determine the position of the current selector within its parent using jQuery? I need this information in order to effectively use the .insertAfter() and .insertBefore() methods to rearrange elements within their nested structu ...

Angular JS sending a message to various views, including a pop-up modal

One of the services I have created is designed to manage message broadcasting to controllers that require it. The service implementation: .factory('mySharedService', function($rootScope) { var sharedService = {}; sharedService.message = &a ...

Executing API POST requests by iterating over an array in ECMAScript 6

My dilemma lies in processing an input that follows the format of "ABC, DEF, GHI, ...." The task at hand is to convert this comma-separated list into an array and make a POST request for each value in the array using my API. I'm seeking guidance on t ...

Are there any callback functions available for the CKEditor Upload Image addon?

Incorporating CKeditor into my project has been a success, but now I have a new task of integrating image uploads as well. After downloading the Addon and setting it up to communicate with the server, everything seemed to be functioning properly. All I ...

Transforming a select dropdown from multiple selection to single selection

Trying to create a scenario where the second dropdown menu in my HTML changes based on the selection from the first dropdown. If "Multiple" is selected in the first dropdown, then the second dropdown should have the attribute multiple="multiple". If "Singl ...

exploring the realm of creating constants in AngularJS functions

controller.js angular.module('app.main') .controller('MainCtrl', function ($scope, currentUser, addAPI) { $scope.form = {}; $scope.subdomain = currentUser.domainName; $scope.add = function () { addAPI.addAdmin(loc ...

What is the best way to incorporate modules into the client side of TypeScript projects?

I'm currently developing a TypeScript project for client-side JavaScript code. Prior to using TypeScript, I used to import a module in vanilla ES6 JavaScript like this: import * as THREE from 'https://threejs.org/build/three.module.js'; H ...

navigating between AngularJS and Symfony 2

Wondering about integrating routing for a Symfony application in AngularJS. I already have the templates for my Symfony app and looking to use AngularJS for routing these templates. Appreciate any help on this! ...

Create an animation where an element glides smoothly over the others, extending to a specific width

How can I make a table of headings stretch to the width of the table and then slide down over the others when one of the headings is clicked? I want the heading to return to its original state when clicked again or when another heading is clicked. Here&ap ...

Unable to execute multiple instances of Selenium PhantomJS concurrently

I have encountered an issue while using Selenium's node.js API to run PhantomJS instances against a series of web pages. The code that I have written to perform actions on the pages is functioning correctly, but it appears that only one instance of Se ...

Activate the input autofocus feature when displaying a dialog in Vue.js

When opening a dialog with input text using v-menu upon clicking a button, how can I focus on the input text field? I have attempted to use $ref but it does not seem to work. newFolderClick(){ this.$refs["input_new_folder"].focus(); //it still appea ...

I am struggling to send an email using PHP mailer with POST data

i'm facing challenges with integrating phpmailer and ajax. As a beginner in ajax, I still have some gaps in understanding the concept. When I directly run my php mailer script on a page with preset values, I can successfully send out an email. However ...

Ways to retrieve the clicked item's index in a jQuery collection without using the index() method

Is there a way to obtain the index of clicked elements easily? var $inputs = $("input"); $inputs.click(function() { console.log($(this).index()) }); This method correctly works for the structure below: div>(input+input+input) However, I am encoun ...

Is there a way to extract all the data values starting with "data-" from the selected input fields in HTML5 and compile them into an object?

Looking for a way to automate input values: <input class="form" type="checkbox" name="item1" data-value="{ 'item1':'selected', 'price': 100 }" checked="checked"> <input class="form" type="checkbox" name="item2" data- ...

Tips for utilizing a unique JavaScript function with Mongoose's findByIdAndUpdate by incorporating $set:

Is it possible to update a database document using a custom JavaScript function? I am aware of the traditional method where you find -> update the document with JavaScript -> save, but this can lead to issues with concurrent data updates. Below is an ...

Tips for transferring data from a controller to $scope in the MVC architecture with AngularJS

I'm a beginner when it comes to AngularJS, currently working with an MVC5 application. I have set up a module and controller in the Angular JS for this application. Within my customer controller in MVC5, I have an action called "View" where I need to ...

Utilize a JavaScript array to showcase particular HTML divisions

With my HTML page using Django, I have a table that contains checkboxes: {% for o in obj %} <input class='germ_ids' name='{{o.id}}'> {% endfor %} <script> var elements = document.getElementsByClassName("germ_id ...

Creating fluid motion with a bezier curve in ThreeJS

I am looking to animate a bezier curve in ThreeJS with updating start, end, and control points. Eventually, I will have multiple curves animating simultaneously. What is the most efficient approach to achieve this? When running the code snippet below, you ...

I continue to encounter a TypeError when using the mongoose-paginate function

Every time I try to run my code, I encounter the following error message: TypeError undefined paginate function Below is the snippet of code causing the issue: var mongoose = require('mongoose'); var mongoosastic = require('mongoosastic ...

Find the first element in an array of resolved promises in sequential order

In my current setup, I am sending multiple requests to different APIs and each request takes a specific amount of time to fulfill. If one request is successful, I return the resolved value and stop. Currently, I am processing these requests one after the o ...