Skip to content

Commit bf66ba3

Browse files
chore: sort by order in filter function
1 parent fae5a61 commit bf66ba3

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/filter.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ function filter($data, Request $req): array {
2828
/** Search Query Params */
2929
$search ??= null;
3030

31-
// Search Column Fetch All Data
31+
/** Sort Query Params */
32+
$sort ??= null;
33+
34+
/** Sort Order Query Params */
35+
$order ??= 'asc'; // asc or desc
36+
37+
// Search All Column Fetch All Data
3238
if ($search):
3339
foreach ($data as $item):
3440
$isSearch = false;
@@ -46,11 +52,19 @@ function filter($data, Request $req): array {
4652
endforeach;
4753
endif;
4854

55+
// Sorted Column Fetch All Data
56+
if ($sort):
57+
$filterData = empty($filterData) ? $data : $filterData;
58+
59+
usort($filterData, fn($a, $b) =>
60+
$order == 'asc' ? $a->$sort > $b->$sort : $a->$sort < $b->$sort);
61+
endif;
62+
4963
// Selected Column Fetch All Data
5064
if ($filter):
5165
$filter = explode(",", $filter);
5266

53-
if ($search)
67+
if ($search || $sort)
5468
$data = $filterData;
5569

5670
$filterData = [];
@@ -61,8 +75,9 @@ function filter($data, Request $req): array {
6175

6276
// According Pagination Fetch All Data
6377
if ($page):
64-
if ($filter || $search)
78+
if ($filter || $search || $sort)
6579
$data = $filterData;
80+
6681
$filterData = array_slice($data, $page == 1 ? 0 : $limit * ($page - 1), (int) $limit);
6782
endif;
6883

0 commit comments

Comments
 (0)