
- 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 string of words based on the number present in each word using JavaScript
Problem
We are required to write a JavaScript function that takes in a string that represents a sentence. Our function should sort this sentence.
Each word in the sentence string contains an integer. Our function should sort the string such that the word that contains the smallest integer is placed first and then in the increasing order.
Example
Following is the code −
const str = "is2 Thi1s T4est 3a"; const sortByNumber = (str = '') => { const findNumber = (s = '') => s .split('') .reduce((acc, val) => +val ? +val : acc, 0); const arr = str.split(' '); const sorter = (a, b) => { return findNumber(a) - findNumber(b); }; arr.sort(sorter); return arr.join(' '); }; console.log(sortByNumber(str));
Output
Thi1s is2 3a T4est
- Related Articles
- Sorting words by last character present in them in JavaScript
- Sorting Array based on another array JavaScript
- Reversing words present in a string in JavaScript
- Repeating each character number of times their one based index in a string using JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Sorting numbers based on their digit sums in JavaScript
- Order an array of words based on another array of words JavaScript
- How to capitalize the first letter of each word in a string using JavaScript?
- Finding the number of words in a string JavaScript
- Python Program to Calculate the Number of Words and the Number of Characters Present in a String
- Encrypting a string based on an algorithm using JavaScript
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- Sorting string alphabetically and inserting underscores using JavaScript

Advertisements