Saturday, May 25, 2019
Javascript Basics
Sunday, August 13, 2017
jQuery Presentation
jQuery Basics
What is Jquery?
Friday, August 23, 2019
Create a Digital Clock using HTML and JavaScript
Create a Digital Clock using HTML and JavaScript
Saturday, November 4, 2023
Sorting on the basis of multiple fields
function sortObjects(arr) {
arr.sort(function(a, b) { if (a.adviserid < b.adviserid) { return -1; } else if (a.adviserid > b.adviserid) { return 1; } else { if (a.cutomer_id < b.cutomer_id) { return -1; } else if (a.cutomer_id > b.cutomer_id) { return 1; } else { if (a.account_id < b.account_id) { return -1; } else if (a.account_id > b.account_id) { return 1; } else { if (a.product_type === b.product_type) { if (a.account_id > b.account_id) { return -1; } else if (a.account_id < b.account_id) { return 1; } } return 0; } } } }); // Group the array by adviserid var groupedArr = []; var currentAdviserId = null; for (var i = 0; i < arr.length; i++) { var obj = arr[i]; if (obj.adviserid !== currentAdviserId) { currentAdviserId = obj.adviserid; groupedArr.push([]); } groupedArr[groupedArr.length - 1].push(obj); } return groupedArr; }
Here's how you would use this function:
// Example array of objects var arr = [ { adviserid: 1, cutomer_id: "abc", account_id: 10, product_type: "A" }, { adviserid: 1, cutomer_id: "abc", account_id: 20, product_type: "A" }, { adviserid: 1, cutomer_id: "def", account_id: 30, product_type: "B" }, { adviserid: 2, cutomer_id: "ghi", account_id: 40, product_type: "A" } ]; // Call the function to sort the array var sortedArr = sortObjects(arr); // Print the sorted array console.log(sortedArr);
In this example, the arr
array contains four objects with adviserid
, cutomer_id
, account_id
, and product_type
properties. The sortObjects
function sorts the array based on the three fields you specified (in ascending order), and then groups the sorted array by adviserid
. The sorted and grouped array is then printed to the console.
In the sorting logic, the function first sorts by adviserid
, then by cutomer_id
, and then by account_id
. If two objects have the same product_type
, they are sorted by account_id
in descending order. If two objects have the same adviserid
, they are grouped together in the output array.
Sure, here's an example JavaScript function that sorts an array of objects based on the three fields you specified:
function sortObjects(arr) { arr.sort(function(a, b) { if (a.adviserId < b.adviserId) { return -1; } else if (a.adviserId > b.adviserId) { return 1; } else { if (a.priority < b.priority) { return -1; } else if (a.priority > b.priority) { return 1; } else { if (a.accountId < b.accountId) { return 1; } else if (a.accountId > b.accountId) { return -1; } else { return 0; } } } }); return arr; } // Example array of objects var arr = [ { adviserId: 2, priority: 1, accountId: 10 }, { adviserId: 1, priority: 2, accountId: 20 }, { adviserId: 1, priority: 2, accountId: 30 }, { adviserId: 2, priority: 1, accountId: 40 } ]; // Print the input array console.log("Input: ", arr); // Call the function to sort the array var sortedArr = sortObjects(arr); // Print the sorted array console.log("Output: ", sortedArr);
In this example, the arr
array contains four objects with adviserId
, priority
, and accountId
properties. The sortObjects
function sorts the array based on the three fields you specified, with priority being sorted in ascending order. If two objects have the same priority
, they are sorted by accountId
in descending order. The input and output arrays are then printed to the console.
The output of this function will be:
Input: [ { adviserId: 2, priority: 1, accountId: 10 }, { adviserId: 1, priority: 2, accountId: 20 }, { adviserId: 1, priority: 2, accountId: 30 }, { adviserId: 2, priority: 1, accountId: 40 } ] Output: [ { adviserId: 1, priority: 2, accountId: 30 }, { adviserId: 1, priority: 2, accountId: 20 }, { adviserId: 2, priority: 1, accountId: 40 }, { adviserId: 2, priority: 1, accountId: 10 } ]
Create a Digital Clock using HTML and JavaScript
Create a Digital Clock using HTML and JavaScript <! DOCTYPE html> < html > < head > ...
Followers
Search This Blog
Popular Posts
-
MG-CEIT Course Feedback Form Step1: Like below three pages for the updated course information ...
-
Software Development Tools Presentation From the moment you begin developing software, whether as a freelancer for a startup or wo...
-
कोरोना को जाने प्रश्न (1) :- क्या कोरोना वायरस को ख़त्म किया जा सकता है उत्तर:- नहीं! कोरोना वायर...
-
CDAC Certifications Courses List IT Applications Certificate Course in Business Computing Certificate Course in Global su...
-
Question 1 Which framework is most commonly used for unit testing in Java? JUnit TestNG Mockito Selenium Answer: JUnit Question 2 I...
-
Use Request Dispatcher Servlet in creating simple Login Form We are going to create a Login Form Here. At the end of this section, y...
-
Most Important Java Interview Questions Core Java , JDBC , Spring , Hibernate , Servlet , JSP Interview Questions
-
Create a Digital Clock using HTML and JavaScript <! DOCTYPE html> < html > < head > ...
-
import redis from 'redis'; // Create a Redis client with retry strategy const client = redis.createClient({ host: 'localhos...
-
const redis = require('redis'); const { promisify } = require('util'); // Create a Redis client with a connection timeout...