From 41a0c291c3f82201f17fe5b6a75bdcbe53d60181 Mon Sep 17 00:00:00 2001 From: izica Date: Tue, 16 Apr 2019 17:51:52 +0700 Subject: [PATCH] Update PhpCollection.php --- PhpCollection.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/PhpCollection.php b/PhpCollection.php index d704464..7330962 100644 --- a/PhpCollection.php +++ b/PhpCollection.php @@ -306,8 +306,13 @@ public function sortBy($key, $asc = true) * @param $function * @return PhpCollection */ - public function sort($function) + public function sort($function = false) { + if ($function === false) { + $function = function ($a, $b) { + return $a > $b ? +1 : -1; + }; + } $arData = $this->arCollection; uasort($arData, $function); return new PhpCollection($arData); @@ -375,25 +380,25 @@ public function merge($array) } /** - * @param $array * @return PhpCollection */ public function unique() { $arBuffer = []; $arResult = []; - foreach ($this->arCollection as $arItem){ - $sHash = md5($arItem); - if(isset($arBuffer[$sHash])){ - if($arBuffer[$sHash] !== json_encode($arItem)){ + foreach ($this->arCollection as $arItem) { + $sJson = json_encode($arItem); + $sHash = md5($sJson); + if (isset($arBuffer[$sHash])) { + if ($arBuffer[$sHash] !== $sJson) { $arResult[] = $arItem; } - }else{ - $arBuffer[$sHash] = json_encode($arItem); + } else { + $arBuffer[$sHash] = $sJson; $arResult[] = $arItem; } } return new PhpCollection($arResult); } -} \ No newline at end of file +}