- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Push specific elements to last in JavaScript
Suppose we have an array of objects like this −
const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ];
We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions −
If arr.flag === false, the matching element gets placed first in the array, but only after the previous matching elements.
The elements that do not match, remain in the same order they were in originally.
Order of appearance is important.
So, for the above array, the output should be −
const output = [ {flag: false, other: 3}, {flag: false, other: 7}, {flag: true, other: 1}, {flag: true, other: 2}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6} ];
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ]; const sortByFlag = arr => { const sorter = (a, b) => { if(!a['flag'] && b['flag']){ return -1; }; if(a['flag'] && !b['flag']){ return 1; } return a['other'] - b['other']; } arr.sort(sorter); }; sortByFlag(arr); console.log(arr);
Output
The output in the console will be −
[ { flag: false, other: 3 }, { flag: false, other: 7 }, { flag: true, other: 1 }, { flag: true, other: 2 }, { flag: true, other: 4 }, { flag: true, other: 5 }, { flag: true, other: 6 } ]
- Related Articles
- First and last child node of a specific node in JavaScript?
- How to push an element to the last of an array in TypeScript?
- Push() vs unshift() in JavaScript?
- How to merge specific elements inside an array together - JavaScript
- Shift last given number of elements to front of array JavaScript
- Not able to push all elements of a stack into another stack using for loop in JavaScript?
- How to push and pop elements in a queue in Ruby?
- JavaScript to push value in empty index in array
- Validating push pop sequence in JavaScript
- How do I push elements to an existing array in MongoDB?
- Finding two closest elements to a specific number in an array using JavaScript
- Difference between push() and unshift() methods in javascript
- Creating an associative array in JavaScript with push()?
- How do find out all elements that match a specific condition in JavaScript?
- MySQL query to exclude values having specific last 3 digits

Advertisements