Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Sorting with map": clarify example #3133

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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