
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sorting digits of all the number of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (lets say in ascending order for the sake of this problem).
For example − If the array is −
const arr = [543, 65, 343, 75, 567, 878, 87];
Then the output should be −
const output = [345, 56, 334, 57, 567, 788, 78];
Example
Following is the code −
const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => { const numArr = String(num).split('').map(el => +el); numArr.sort((a, b) => a - b); return numArr.join(''); }; const sortDigits = arr => { const res = []; for(let i = 0; i < arr.length; i++){ res.push(ascendNumber(arr[i])); }; return res; }; console.log(sortDigits(arr));
Output
Following is the output in the console −
[ '345', '56', '334', '57', '567', '788', '78' ]
- Related Articles
- Sorting array of Number by increasing frequency JavaScript
- Recursive sum all the digits of a number JavaScript
- Recursive product of all digits of a number - JavaScript
- Return an array populated with the place values of all the digits of a number in JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Convert number to reversed array of digits JavaScript
- Reversed array of digits from number using JavaScript
- Uneven sorting of array in JavaScript
- Convert number to a reversed array of digits in JavaScript
- Sorting an array of binary values - JavaScript
- Alternative sorting of an array in JavaScript
- Sorting parts of array separately in JavaScript
- Sorting an array of objects by an array JavaScript
- Sorting JavaScript object by length of array properties.
- Sorting only a part of an array JavaScript

Advertisements