Jeremie Ges
Jeremie Ges

Reputation: 2743

ActiveRecord - Order by alphabet first then follow by number

Let's say I have a collection products with the following values for "name":

The following code:

@products.reorder('name ASC') # I really need to use reorder in my code

will list the results as-is:

What should I tweak in reorder() to have the following order:

Upvotes: 1

Views: 438

Answers (2)

codenamev
codenamev

Reputation: 2208

@products.reorder("(name !~* '^[a-z]'), name")

Upvotes: 0

Alex Baidan
Alex Baidan

Reputation: 1075

@products.reorder("(name ~ '^[0-9]'), name")

Explanation: FALSE sorts before TRUE so digit values will be last.

Upvotes: 1

Related Questions