Skip to content

Commit

Permalink
Merge pull request #15518 from snipe/sort_by_numeric_custom_fields
Browse files Browse the repository at this point in the history
Fixed #11634 - Sort by numeric columns for numeric custom fields
  • Loading branch information
snipe authored Sep 19, 2024
2 parents eef487d + 7c85ad5 commit 454796e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,29 @@ public function index(Request $request, $action = null, $upcoming_status = null)
$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;
}

Expand Down

0 comments on commit 454796e

Please sign in to comment.