
- 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 the numbers from within in 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 (let’s 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];
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
[ '345', '56', '334', '57', '567', '788', '78' ]
- Related Articles
- Sorting alphabets within a string in JavaScript
- Sorting numbers according to the digit root JavaScript
- Prime numbers within a range in JavaScript
- Sorting according to weights of numbers in JavaScript
- Sorting numbers based on their digit sums in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Algorithm for sorting array of numbers into sets in JavaScript
- Referring JavaScript function from within itself
- Sorting numbers in descending order but with `0`s at the start JavaScript
- Summing cubes of natural numbers within a range in JavaScript
- Finding sum of all numbers within a range in JavaScript
- Relative sorting in JavaScript
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Fetch Second minimum element from an array without sorting JavaScript
- Finding the count of numbers divisible by a number within a range using JavaScript

Advertisements