Skip to content

Commit e6d42d9

Browse files
committed
fix: sort direction match usort
1 parent c63365d commit e6d42d9

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ This means that if two elements have the same key, the one that appeared earlier
332332
in the input will also appear earlier in the sorted output.
333333

334334
```php
335-
$valueObjectFactory = static fn (int $id, int $weight) => new class($id, $weight)
335+
$valueObjectFactory = static fn (int $id, int $weight): object => new class($id, $weight)
336336
{
337337
public function __construct(public readonly int $id, public readonly int $weight) {}
338338
};

src/SortIterableAggregate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(iterable $iterable, private Closure $callback)
5454
*/
5555
protected function compare($value1, $value2): int
5656
{
57-
return (0 === $return = ($this->callback)($value1[1][1], $value2[1][1], $value1[1][0], $value2[1][0])) ? $value2[0] <=> $value1[0] : $return;
57+
return (0 === $return = ($this->callback)($value2[1][1], $value1[1][1], $value2[1][0], $value1[1][0])) ? $value2[0] <=> $value1[0] : $return;
5858
}
5959
};
6060

tests/unit/SortIterableAggregateTest.php

+54-25
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,82 @@ final class SortIterableAggregateTest extends TestCase
2222
{
2323
public function testDoNothing(): void
2424
{
25-
$input = ['a', 'b', 'c', 'd', 'e', 'f'];
25+
$input = range('a', 'f');
2626

27-
$iterator = (new SortIterableAggregate($input, static fn ($right, $left): int => $left <=> $right));
27+
$iterator = (new SortIterableAggregate($input, static fn (string $left, string $right): int => $left <=> $right));
2828

29-
$expected = [];
29+
$actual = array_reduce(
30+
iterator_to_array($iterator),
31+
static fn (array $carry, string $item): array => [...$carry, $item],
32+
[]
33+
);
3034

31-
foreach ($iterator as $value) {
32-
$expected[] = $value;
33-
}
34-
35-
self::assertSame($expected, $input);
35+
self::assertSame($input, $actual);
3636
}
3737

3838
public function testSimpleSort(): void
3939
{
4040
$input = array_combine(range('c', 'a'), range('c', 'a'));
4141

42-
$iterator = (new SortIterableAggregate($input, static fn ($right, $left): int => $left <=> $right));
42+
$iterator = (new SortIterableAggregate($input, static fn (string $left, string $right): int => $left <=> $right));
43+
44+
$actual = array_reduce(
45+
iterator_to_array($iterator),
46+
static fn (array $carry, string $item): array => [...$carry, $item],
47+
[]
48+
);
49+
50+
self::assertSame(range('a', 'c'), $actual);
51+
}
52+
53+
public function testSortingDirectionMatchUsort(): void
54+
{
55+
$input = [
56+
self::createValueObject(id: 1, weight: 2),
57+
self::createValueObject(id: 160, weight: 1),
58+
self::createValueObject(id: 1600, weight: 3),
59+
self::createValueObject(id: 2, weight: 2),
60+
self::createValueObject(id: 150, weight: 1),
61+
self::createValueObject(id: 1500, weight: 3),
62+
self::createValueObject(id: 3, weight: 2),
63+
];
4364

44-
$expected = [];
65+
$sortCallback = static fn (object $a, object $b): int => $a->weight <=> $b->weight;
4566

46-
foreach ($iterator as $value) {
47-
$expected[] = $value;
48-
}
67+
$actual = array_reduce(
68+
iterator_to_array(new SortIterableAggregate($input, $sortCallback)),
69+
static fn (array $carry, object $item): array => [...$carry, $item->id],
70+
[]
71+
);
4972

50-
self::assertSame($expected, range('a', 'c'));
73+
usort($input, $sortCallback);
74+
$expected = array_map(static fn (object $item): int => $item->id, $input);
75+
76+
self::assertEquals($expected, $actual);
5177
}
5278

5379
public function testStableSort(): void
5480
{
55-
$valueObjectFactory = static fn (int $id, int $weight) => new class($id, $weight) {
56-
public function __construct(
57-
public readonly int $id,
58-
public readonly int $weight,
59-
) {}
60-
};
61-
6281
$input = [
63-
$valueObjectFactory(id: 1, weight: 1),
64-
$valueObjectFactory(id: 2, weight: 1),
65-
$valueObjectFactory(id: 3, weight: 1),
82+
self::createValueObject(id: 1, weight: 1),
83+
self::createValueObject(id: 2, weight: 1),
84+
self::createValueObject(id: 3, weight: 1),
6685
];
6786

6887
$sort = new SortIterableAggregate($input, static fn (object $a, object $b): int => $a->weight <=> $b->weight);
6988

70-
$expected = [1, 2, 3];
89+
$expected = range(1, 3);
7190

7291
self::assertSame($expected, iterator_to_array(new MapIterableAggregate($sort, static fn (object $value): int => $value->id)));
7392
}
93+
94+
private static function createValueObject(int $id, int $weight): object
95+
{
96+
return new class($id, $weight) {
97+
public function __construct(
98+
public int $id,
99+
public int $weight,
100+
) {}
101+
};
102+
}
74103
}

0 commit comments

Comments
 (0)