Beta

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 or undefined. 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. Any compareFunction returned this way should be efficient, that is to say, sorterAwaitingOrder("asc") will always return the same compareFunction as sorterAwaitingOrder("asc") or sorterAwaitingOrder("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]
Algorithms
Sorting
Arrays

Stats:

CreatedJul 1, 2014
PublishedJul 1, 2014
Warriors Trained96
Total Skips41
Total Code Submissions275
Total Times Completed19
JavaScript Completions19
Total Stars2
% of votes with a positive feedback rating67% of 9
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes2
Total Rank Assessments8
Average Assessed Rank
5 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • wthit56 Avatar
  • Freywar Avatar
  • Voile Avatar
Ad