
- 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 Array without using sort() in JavaScript
We are required to write a JavaScript function that takes in an array of numbers.
The function should sort the array using the Array.prototype.sort() method, but, here, we are required to use the Array.prototype.reduce() method to sort the array.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < arr.length && val < arr[ind]){ ind++; } acc.splice(ind, 0, val); return acc; }, []); }; console.log(sortWithReduce(arr));
Output
The output in the console will be −
[ 98, 57, 89, 37, 34, 5, 56, 4, 3 ]
- Related Articles
- Sorting an array of literals using quick sort in JavaScript
- Sorting arrays using bubble sort in JavaScript
- Count unique elements in array without sorting JavaScript
- Sorting an integer without using string methods and without using arrays in JavaScript
- Unique sort (removing duplicates and sorting an array) in JavaScript
- How to sorting an array without using loops in Node.js?
- Fetch Second minimum element from an array without sorting JavaScript
- Check if items in an array are consecutive but WITHOUT SORTING in JavaScript
- Using merge sort to recursive sort an array JavaScript
- Uneven sorting of array in JavaScript
- How to sort 0,1 in an Array without using any extra space using C#?
- JavaScript array sorting by level
- Sorting Array based on another array JavaScript
- Alphanumeric sorting using JavaScript
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript

Advertisements