Skip to content

Commit 6c553a7

Browse files
amongertaylorotwell
authored andcommitted
Fixes input order issue (laravel#14381)
1 parent 4835c0a commit 6c553a7

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/Illuminate/Support/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function median($key = null)
9595
}
9696

9797
$values = with(isset($key) ? $this->pluck($key) : $this)
98-
->values()->sort();
98+
->sort()->values();
9999

100100
$middle = (int) floor($count / 2);
101101

tests/Support/SupportCollectionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,16 @@ public function testEvenMedianCollection()
13101310
$this->assertEquals(1.5, $collection->median('foo'));
13111311
}
13121312

1313+
public function testMedianOutOfOrderCollection()
1314+
{
1315+
$collection = new Collection([
1316+
(object) ['foo' => 0],
1317+
(object) ['foo' => 5],
1318+
(object) ['foo' => 3],
1319+
]);
1320+
$this->assertEquals(3, $collection->median('foo'));
1321+
}
1322+
13131323
public function testMedianOnEmptyCollectionReturnsNull()
13141324
{
13151325
$collection = new Collection();

0 commit comments

Comments
 (0)