@include test-module('_sort') {
    $list: 4 2 5 3 1;

    @include test('should sort a list ascending by default') {
        @include assert-equal(_sort($list), 1 2 3 4 5);
    }

    @include test('should accept keywords for ascending and descending sort') {
        @include assert-equal(_sort($list, 'asc'), 1 2 3 4 5);
        @include assert-equal(_sort($list, 'desc'), 5 4 3 2 1);
    }

    @include test('should work with strings') {
        @include assert-lists-equal(_sort('the' 'big' 'brown' 'fox'), 'big' 'brown' 'fox' 'the');

        @include assert-lists-equal(_sort('the' 'big' 'brown' 'fox', 'desc'), 'the' 'fox' 'brown' 'big');
    }
}

@function test-sort-by-1($a, $b, $c) {
  @return ('a': $a, 'b': $b, 'c': $c);
}
@function test-sort-by-2($map, $args...) {
  @return __get($map, 'b');
}
@function test-sort-by-3($map, $args...) {
  @return __get($map, 'a');
}
@function test-sort-by-4($value, $args...) {
  @return -1 * $value;
}
@include test-module('_sort-by') {
  $maps: (
    ( 'a': 'x', 'b': 3 ),
    ( 'a': 'y', 'b': 4 ),
    ( 'a': 'x', 'b': 1 ),
    ( 'a': 'y', 'b': 2 )
  );

  $stable-order: (
    test-sort-by-1(1, 1, 1), test-sort-by-1(1, 2, 1),
    test-sort-by-1(1, 1, 1), test-sort-by-1(1, 2, 1),
    test-sort-by-1(1, 3, 1), test-sort-by-1(1, 4, 1),
    test-sort-by-1(1, 5, 1), test-sort-by-1(1, 6, 1),
    test-sort-by-1(2, 1, 2), test-sort-by-1(2, 2, 2),
    test-sort-by-1(2, 3, 2), test-sort-by-1(2, 4, 2),
    test-sort-by-1(2, 5, 2), test-sort-by-1(2, 6, 2),
    test-sort-by-1(3, 1, 1), test-sort-by-1(3, 2, 1),
    test-sort-by-1(3, 3, 1), test-sort-by-1(3, 4, 1),
    test-sort-by-1(3, 5, 1), test-sort-by-1(3, 6, 1)
  );

  @include test('should sort in ascending order') {    
    $actual: _pluck(_sort-by($maps, test-sort-by-2), 'b');

    @include assert-lists-equal($actual, 1 2 3 4);
  }
  @include test('should perform a stable sort') {
    $actual: _sort-by($stable-order, test-sort-by-3);

    @include assert-lists-equal($actual, $stable-order);
  }
  @include test('should use _identity when iteratee is nullish') {
    $list: 3 2 1;

    @include assert-true(test-lists-equal(_sort-by($list), 1 2 3));
  }
  @include test('should use _identity when iteratee is nullish') {
    $list: 3 2 1;

    @include assert-true(test-lists-equal(_sort-by($list, null), 1 2 3));
  }

  // @include test('should move null values to the end') {
  //   $list: null 4 1 null 3 null 2;
  //   @debug inspect(_sort-by($list));

  //   @include assert-lists-equal(_sort-by($list), 1 2 3 4 null null null);
  // }

  @include test('should work with a _pluck style iteratee') {
    @include assert-true(test-lists-equal(_pluck(_sort-by($maps, 'b'), 'b'), (1 2 3 4)));
  }

  @include test('should work with a map for collection') {
    @include assert-true(test-lists-equal(_sort-by(('a': 3, 'b': 1, 'c': 2)), 1 2 3));
  }

  @include test('should treat number values for collection as empty') {
    @include assert-true(test-lists-equal(_sort-by(1), ()));
  }

  @include test('should work as an iteratee for _map') {
    $actual: _map((2 1 3, 3 2 1), _sort-by);

    @include assert-true(test-lists-equal($actual, (1 2 3, 1 2 3)));
  }

  // @include test('_sort-by-all should sort multiple properties in ascending order') {
  //   @include assert-equal(
  //     _sort-by-all($maps, 'a' 'b'),
  //     nth($maps, 3) nth($maps, 1) nth($maps, 4) nth($maps, 2));
  // }

  @include test('_sort-by-all should perform a stable sort') {
    @include assert-true(test-lists-equal(_sort-by-all($stable-order, 'a' 'c'), $stable-order));
  }

  @include test('should work with native functions') {
    @include assert-equal(_sort-by(black white yellow purple, lightness), black purple yellow white);
  }

  @include test('should work with a _pluck style predicate') {
    $users: (
      ( 'user': 'fred' ),
      ( 'user': 'pebbles' ),
      ( 'user': 'barney' )
    );
    $actual: _pluck(_sort-by($users, 'user'), 'user');
    $expected: 'barney' 'fred' 'pebbles';

    @include assert-lists-equal($actual, $expected);
  }
}


// @include test-module('_sorted-index methods') {
//   @each $method-name in 'sorted-index', 'sorted-last-index' {
//     $list: 30 50;
//     $func: unquote('_#{$method-name}');
//     $is-sorted-index: $method-name == 'sorted-index';
//     $maps: (('x': 30), ('x': 50));

//     @include test('#{$func} should return the correct insert index') {
//         $values: 30 40 50;
//         $expected: if($is-sorted-index, 1 2 2, 2 2 3);
//         $result: ();

//         @each $value in $values {
//             $result: append($result, _exec($func, $list, $value));
//         }

//         @include assert-true(test-lists-equal($result, $expected));
//     }

//     @include test('#{$func} should work with a list of strings') {
//         $list: 'a' 'c';
//         $values: 'a' 'b' 'c';
//         $expected: if($is-sorted-index, 1 2 2, 2 2 3);
//         $result: ();

//         @each $value in $values {
//             $result: append($result, _exec($func, $list, $value));
//         }

//         @include assert-true(test-lists-equal($result, $expected));
//     }
//   }
// }