Completed
Pull Request — master (#7)
by Sérgio
03:26
created

sort.php ➔ sort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
c 2
b 0
f 1
nc 2
nop 1
dl 0
loc 19
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
/**
6
 * @author Sérgio Rafael Siqueira <[email protected]>
7
 *
8
 * @param array $xss
9
 *
10
 * @return array
11
 */
12
function sort(array $xss)
13
{
14
    $xss = array_values($xss);
15
16
    if (!has(1, $xss)) {
17
        return $xss;
18
    }
19
20
    $pivot = head($xss);
21
    $xs = tail($xss);
22
    $left = array_filter($xs, function ($x) use ($pivot) {
23
        return $x < $pivot;
24
    });
25
    $right = array_filter($xs, function ($x) use ($pivot) {
26
        return $x > $pivot;
27
    });
28
29
    return array_merge(sort($left), [$pivot], sort($right));
30
}
31