Skip to content

Commit

Permalink
"Sorting with map": clarify example (#3133)
Browse files Browse the repository at this point in the history
Fixes #3115
  • Loading branch information
vitaly-zdanevich authored Mar 15, 2021
1 parent 82a7fbd commit bb8c53c
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ <h3 id="Sorting_with_map">Sorting with map</h3>
temporary array to achieve the right order.</p>

<pre class="brush: js">// the array to be sorted
var list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];
const in = ['delta', 'alpha', 'charlie', 'bravo'];

// temporary array holds objects with position and sort-value
var mapped = list.map(function(el, i) {
return { index: i, value: el.toLowerCase() };
const mapped = in.map((v, i) => {
return { i, value: someSlowOperation(v) };
})

// sorting the mapped array containing the reduced values
mapped.sort(function(a, b) {
mapped.sort((a, b) => {
if (a.value &gt; b.value) {
return 1;
}
Expand All @@ -239,10 +239,7 @@ <h3 id="Sorting_with_map">Sorting with map</h3>
return 0;
});

// container for the resulting order
var result = mapped.map(function(el){
return list[el.index];
});
const result = mapped.map(v => in[v.i]);
</pre>

<p>There is an open source library available called <a
Expand Down

0 comments on commit bb8c53c

Please sign in to comment.