- 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
Sorting binary string having an even decimal value using JavaScript
Problem
We are required to write a JavaScript function that takes in a string that contains binary strings of length 3 all separated by spaces.
Our function should sort the numbers in ascending order but only order the even numbers and leave all odd numbers in their place.
Example
Following is the code −
const str = '101 111 100 001 010'; const sortEvenIncreasing = (str = '') => { const sorter = (a, b) => { const findInteger = bi => parseInt(bi, 2); if(findInteger(a) % 2 === 1 || findInteger(b) % 2 === 1){ return 0; }; return findInteger(a) - findInteger(b); }; const res = str .split(' ') .sort(sorter) .join(' '); return res; }; console.log(sortEvenIncreasing(str));
Output
101 111 100 001 010
- Related Articles
- Counting even decimal value substrings in a binary string in C++
- Sorting an array object by property having falsy value - JavaScript
- Sorting an array objects by property having null value in JavaScript
- Sorting an array of binary values - JavaScript
- Sorting an integer without using string methods and without using arrays in JavaScript
- Decimal to binary conversion using recursion in JavaScript
- Sorting an array that contains the value of some weights using JavaScript
- Sorting odd and even elements separately JavaScript
- Sorting strings with decimal points in JavaScript
- Sorting string alphabetically and inserting underscores using JavaScript
- Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
- Sorting according to number of 1s in binary representation using JavaScript
- Binary to decimal using C#
- Alphanumeric sorting using JavaScript
- Binary array to corresponding decimal in JavaScript

Advertisements