From 7c85ad54eb742f063936423d1c8b21d4f9f57079 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 17 Sep 2024 19:44:03 +0100 Subject: [PATCH] Sort by numeric columns for numeric custom fields Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 1243f1212a..0c80d50e5c 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -372,7 +372,29 @@ class AssetsController extends Controller $assets->OrderAssigned($order); break; default: - $assets->orderBy($column_sort, $order); + $numeric_sort = false; + + // Search through the custom fields array to see if we're sorting on a custom field + if (array_search($column_sort, $all_custom_fields->pluck('db_column')->toArray()) !== false) { + + // Check to see if this is a numeric field type + foreach ($all_custom_fields as $field) { + if (($field->db_column == $sort_override) && ($field->format == 'NUMERIC')) { + $numeric_sort = true; + break; + } + } + + // This may not work for all databases, but it works for MySQL + if ($numeric_sort) { + $assets->orderByRaw($sort_override . ' * 1 ' . $order); + } else { + $assets->orderBy($sort_override, $order); + } + + } else { + $assets->orderBy($column_sort, $order); + } break; }