- 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
Arranging words in Ascending order in a string - JavaScript
Let’s say, we are required to write a JavaScript function that takes in a string and returns a new string with words rearranged according to their increasing length.
Example
Following is the code −
const str = 'This is a sample string only'; const arrangeByLength = str => { const strArr = str.split(' '); const sorted = strArr.sort((a, b) => { return a.length - b.length; }); return sorted.join(' '); }; console.log(arrangeByLength(str));
Output
Following is the output in the console −
a is This only sample string
- Related Articles
- Arranging words by their length in a sentence in JavaScript
- Reversing the order of words of a string in JavaScript
- Java program to sort words of sentence in ascending order
- Python program to sort out words of the sentence in ascending order
- Sorting an associative array in ascending order - JavaScript
- Reversing words in a string in JavaScript
- Finding duplicate "words" in a string - JavaScript
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Reversing words present in a string in JavaScript
- Biggest number by arranging numbers in certain order in C++
- Swapping adjacent words of a String in JavaScript
- Keeping only redundant words in a string in JavaScript
- Replace words of a string - JavaScript
- Reversing words within a string JavaScript
- Finding the number of words in a string JavaScript

Advertisements