Sorter(getter)(order)
Description:
Write a helper function to create a comaparison function for use with the Array's sort method.
For the purposes of this description a "null value" will refer to any value
null
orundefined
. A non-null value will refer to any value that is not a null value.
sorter(getter, order)
The getter
parameter should be a function. This function should be used to extract or transform the value for use the returned compareFunction
. If getter
was a null value (null
or undefined
), the compareFunction
should transform values to strings for sorting.
The order
parameter should be a non-null value (not null
or undefined
). If its value is "asc" or "ascending", the compareFunction
returned, when used in conjuction with an Array's sort method, should result in the items being sorted in an ascending order (lowest to highest). If its value is "desc" or "descending", the compareFunction
should sort in descending order (highest to lowest). If any other non-null value is given, the compareFunction
should sort in ascending order.
sorter(getter)
The getter
parameter should be used as in the setter(getter, order)
signature. There may be a second argument of value null
or undefined
.
An awaitingOrder
function should be returned. This function should accept the following two signatures:
sorter(getter)(order)
Should behave as though
sorter(getter, order)
was called. AnycompareFunction
returned this way should be efficient, that is to say,sorterAwaitingOrder("asc")
will always return the samecompareFunction
assorterAwaitingOrder("asc")
orsorterAwaitingOrder("ascending")
. Same goes for descending, or other non-null values.sorter(getter)(a, b)
Should behave as though
sorter(getter, "asc")(a, b)
was called.
sorter(order)
Should behave as though sorter(null, order)
was called. There may be a second argument of value null
or undefined
.
A compareFunction
should be returned, using the default sorting behavio
sorter()
Should behave as though sorter(null)
was called.
Let's have a look at an example:
var input = [10, 2, 9];
var byValue = sorter(function(v) { return v; });
input.sort(byValue("desc")); // == [10, 9, 2]
input.sort(byValue(null)); // == [2, 9, 10]
byValue("asc") == byValue("ascending"); // == true
input.sort(sorter()("desc")); // == [9, 2, 10]
Similar Kata:
Stats:
Created | Jul 1, 2014 |
Published | Jul 1, 2014 |
Warriors Trained | 96 |
Total Skips | 41 |
Total Code Submissions | 275 |
Total Times Completed | 19 |
JavaScript Completions | 19 |
Total Stars | 2 |
% of votes with a positive feedback rating | 67% of 9 |
Total "Very Satisfied" Votes | 5 |
Total "Somewhat Satisfied" Votes | 2 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 8 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 6 kyu |