diff --git a/src/util/_impl/_sorted_impl.scad b/src/util/_impl/_sorted_impl.scad index ab2a884e..2adf44a7 100644 --- a/src/util/_impl/_sorted_impl.scad +++ b/src/util/_impl/_sorted_impl.scad @@ -21,10 +21,10 @@ function _sorted(lt, less, elem = identity) = ) [each _sorted(b_a[0], less, elem), elem(pivot), each _sorted(b_a[1], less, elem)]; -function _sorted_default(lt) = _sorted(lt, function(a, b) a < b); +function _sorted_default(lt, reverse) = _sorted(lt, reverse ? function(a, b) a > b : function(a, b) a < b); -function _sorted_cmp(lt, cmp) = _sorted(lt, function(a, b) cmp(a, b) < 0); +function _sorted_cmp(lt, cmp, reverse) = _sorted(lt, reverse ? function(a, b) cmp(a, b) > 0 : function(a, b) cmp(a, b) < 0); -function _sorted_key(lt, key) = +function _sorted_key(lt, key, reverse) = let(key_elem_lt = [for(elem = lt) [key(elem), elem]]) - _sorted(key_elem_lt, function(a, b) a[0] < b[0], function(elem) elem[1]); \ No newline at end of file + _sorted(key_elem_lt, reverse ? function(a, b) a[0] > b[0] : function(a, b) a[0] < b[0], function(elem) elem[1]); \ No newline at end of file diff --git a/src/util/sorted.scad b/src/util/sorted.scad index 13e55b3a..cdcae308 100644 --- a/src/util/sorted.scad +++ b/src/util/sorted.scad @@ -10,8 +10,8 @@ use <_impl/_sorted_impl.scad>; -function sorted(lt, cmp = undef, key = undef) = +function sorted(lt, cmp = undef, key = undef, reverse = false) = let(is_cmp_undef = is_undef(cmp)) - is_cmp_undef && is_undef(key) ? _sorted_default(lt) : - is_cmp_undef ? _sorted_key(lt, key) : - _sorted_cmp(lt, cmp); \ No newline at end of file + is_cmp_undef && is_undef(key) ? _sorted_default(lt, reverse) : + is_cmp_undef ? _sorted_key(lt, key, reverse) : + _sorted_cmp(lt, cmp, reverse); \ No newline at end of file diff --git a/test/util/test_sorted.scad b/test/util/test_sorted.scad index d1790ef6..b6020e6b 100644 --- a/test/util/test_sorted.scad +++ b/test/util/test_sorted.scad @@ -4,8 +4,10 @@ module test_sorted() { echo("==== test_sorted ===="); assert([1, 2, 3, 4, 5, 6] == sorted([1, 6, 2, 5, 4, 3])); + assert([6, 5, 4, 3, 2, 1] == sorted([1, 6, 2, 5, 4, 3], reverse = true)); assert(["b", "c", "d", "m", "x"] == sorted(["x", "c", "b", "d", "m"])); + assert(["x", "m", "d", "c", "b"] == sorted(["x", "c", "b", "d", "m"], reverse = true)); assert( [[2, 0, 0], [5, 0, 0], [7, 0, 0], [9, 0, 0], [10, 0, 0]] == @@ -20,12 +22,18 @@ module test_sorted() { ascending = function(e1, e2) e1 - e2; descending = function(e1, e2) e2 - e1; assert(sorted([2, 1, 3, 5, 4], ascending) == [1, 2, 3, 4, 5]); + assert(sorted([2, 1, 3, 5, 4], ascending, reverse = true) == [5, 4, 3, 2, 1]); assert(sorted([2, 1, 3, 5, 4], descending) == [5, 4, 3, 2, 1]); assert( [[2, 0, 0], [5, 0, 0], [7, 0, 0], [9, 0, 0], [10, 0, 0]] == sorted([[10, 0, 0], [5, 0, 0], [7, 0, 0], [2, 0, 0], [9, 0, 0]], key = function(elem) elem.x) ); + + assert( + [[10, 0, 0], [9, 0, 0], [7, 0, 0], [5, 0, 0], [2, 0, 0]] == + sorted([[10, 0, 0], [5, 0, 0], [7, 0, 0], [2, 0, 0], [9, 0, 0]], key = function(elem) elem.x, reverse = true) + ); } test_sorted();