@@ 1134-1154 (lines=21) @@ | ||
1131 | * |
|
1132 | * @return static |
|
1133 | */ |
|
1134 | public function natcasesort(): Collection |
|
1135 | { |
|
1136 | $index = 0; |
|
1137 | $items = $this->items; |
|
1138 | ||
1139 | foreach ($items as &$item) { |
|
1140 | $item = [$index++, $item]; |
|
1141 | } |
|
1142 | ||
1143 | uasort($items, function ($a, $b) { |
|
1144 | $result = strnatcasecmp($a[1], $b[1]); |
|
1145 | ||
1146 | return $result === 0 ? $a[0] - $b[0] : $result; |
|
1147 | }); |
|
1148 | ||
1149 | foreach ($items as &$item) { |
|
1150 | $item = $item[1]; |
|
1151 | } |
|
1152 | ||
1153 | return new static($items); |
|
1154 | } |
|
1155 | ||
1156 | /** |
|
1157 | * Sort an array using a "natural order" algorithm. |
|
@@ 1161-1181 (lines=21) @@ | ||
1158 | * |
|
1159 | * @return static |
|
1160 | */ |
|
1161 | public function natsort(): Collection |
|
1162 | { |
|
1163 | $index = 0; |
|
1164 | $items = $this->items; |
|
1165 | ||
1166 | foreach ($items as &$item) { |
|
1167 | $item = [$index++, $item]; |
|
1168 | } |
|
1169 | ||
1170 | uasort($items, function ($a, $b) { |
|
1171 | $result = strnatcmp($a[1], $b[1]); |
|
1172 | ||
1173 | return $result === 0 ? $a[0] - $b[0] : $result; |
|
1174 | }); |
|
1175 | ||
1176 | foreach ($items as &$item) { |
|
1177 | $item = $item[1]; |
|
1178 | } |
|
1179 | ||
1180 | return new static($items); |
|
1181 | } |
|
1182 | ||
1183 | /** |
|
1184 | * Sort an array with a user-defined comparison function and maintain index association. |
|
@@ 1190-1210 (lines=21) @@ | ||
1187 | * |
|
1188 | * @return static |
|
1189 | */ |
|
1190 | public function uasort(callable $callback): Collection |
|
1191 | { |
|
1192 | $index = 0; |
|
1193 | $items = $this->items; |
|
1194 | ||
1195 | foreach ($items as &$item) { |
|
1196 | $item = [$index++, $item]; |
|
1197 | } |
|
1198 | ||
1199 | uasort($items, function ($a, $b) use ($callback) { |
|
1200 | $result = call_user_func($callback, $a[1], $b[1]); |
|
1201 | ||
1202 | return $result === 0 ? $a[0] - $b[0] : $result; |
|
1203 | }); |
|
1204 | ||
1205 | foreach ($items as &$item) { |
|
1206 | $item = $item[1]; |
|
1207 | } |
|
1208 | ||
1209 | return new static($items); |
|
1210 | } |
|
1211 | ||
1212 | /** |
|
1213 | * Sort an array by keys using a user-defined comparison function. |
|
@@ 1240-1260 (lines=21) @@ | ||
1237 | * |
|
1238 | * @return static |
|
1239 | */ |
|
1240 | public function usort(callable $callback): Collection |
|
1241 | { |
|
1242 | $index = 0; |
|
1243 | $items = $this->items; |
|
1244 | ||
1245 | foreach ($items as &$item) { |
|
1246 | $item = [$index++, $item]; |
|
1247 | } |
|
1248 | ||
1249 | usort($items, function ($a, $b) use ($callback) { |
|
1250 | $result = call_user_func($callback, $a[1], $b[1]); |
|
1251 | ||
1252 | return $result === 0 ? $a[0] - $b[0] : $result; |
|
1253 | }); |
|
1254 | ||
1255 | foreach ($items as &$item) { |
|
1256 | $item = $item[1]; |
|
1257 | } |
|
1258 | ||
1259 | return new static($items); |
|
1260 | } |
|
1261 | ||
1262 | /** |
|
1263 | * Sort the collection using the given callback. |