summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--it.class18
1 files changed, 14 insertions, 4 deletions
diff --git a/it.class b/it.class
index 8f7435c..df6c0c0 100644
--- a/it.class
+++ b/it.class
@@ -1182,12 +1182,22 @@ static function json_decode($json, $p = [])
}
/**
- * Retuns sorted array. mode string can contain combinations of a for assoc, k for key, r for reverse and n for numeric
+ * Retuns sorted array.
+ * @param $mode contains combinations of a for assoc, k for key, r for reverse and n for numeric
+ * @param $compare two arg compare function or single arg map function. $mode can k a "" or omitted
*/
-static function sort($array, $mode = "")
+static function sort($array, $mode = "", $compare = null)
{
- $func = it::replace(array('n' => ""), count_chars($mode, 3)) . "sort"; # count_chars sorts flags
- $func($array, it::match('n', $mode) ? SORT_NUMERIC : 0);
+ if (is_callable($mode) && !(is_string($mode) && strlen($mode) < 3)) # "a" is a func and a mode
+ list($mode, $compare) = [ "", $mode ];
+
+ if (!($arg = $compare))
+ $arg = it::match('n', $mode) ? SORT_NUMERIC : 0;
+ else if (($prefix = "u") && (new ReflectionFunction($compare))->getNumberOfRequiredParameters() == 1)
+ $arg = function($a, $b) use ($compare) { return $compare($a) <=> $compare($b); };
+
+ $func = $prefix . it::replace(array('n' => ""), count_chars($mode, 3)) . "sort"; # count_chars sorts flags
+ $func($array, $arg);
return $array;
}