Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Array with JavaScript reduce function

Tags:

Often I study some JavaScript interview questions, suddenly I saw a question about usage of reduce function for sorting an Array, I read about it in MDN and the usage of it in some medium articles, But sorting an Array is so Innovative:

const arr = [91,4,6,24,8,7,59,3,13,0,11,98,54,23,52,87,4]; 

I thought a lot, but I've no idea about how answer this question, how must be the reduce call back function? what is the initialValue of reduce function? And what are the accumulator and currentValue of call back function of reduce?

And at the end, does this way have some benefits than other sorting algorithms? Or Is it useful to improve other algorithms?

like image 365
AmerllicA Avatar asked May 09 '18 05:05

AmerllicA


People also ask

How do you sort an array in JavaScript using reduce?

sort() method. We are required to use the Array. prototype. reduce() method to sort the array.

What is reduce () in JavaScript?

reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. Syntax: array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

What is the reduce () array method?

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

Can JavaScript reduce return an array?

The array reduce in JavaScript is a predefined method used to reduce an array to a single value by passing a callback function on each element of the array. It accepts a function executed on all the items of the specified array in the left-to-right sequence. The returned single value is stored in the accumulator.


1 Answers

It makes no sense to use reduce here, however you could use a new array as an accumulator and do insertion sort with all elements:

array.reduce((sorted, el) => {   let index = 0;   while(index < sorted.length && el < sorted[index]) index++;   sorted.splice(index, 0, el);   return sorted; }, []); 

Here is the version without reduce:

array.sort((a, b) => a - b); 

Now some general tips for writing reducers:

how must be the reduce call back function?

You either take an approach with an accumulator, then the reducer should apply a modification to the accumulator based on the current element and return it:

(acc, el) => acc 

Or if accumulator and the elements have the sane type and are logically equal, you dont need to distinguish them:

 (a, b) => a + b 

what is the initialValue of reduce function?

You should ask yourself "What should reduce return when it is applied on an empty array?"

Now the most important: When to use reduce? (IMO)

If you want to boil down the values of an array into one single value or object.

like image 143
Jonas Wilms Avatar answered Sep 22 '22 13:09

Jonas Wilms