0

users_controller.eb

def index
 users=User.all
end

index.json.jbuilder

json.users_availability @users.order('name ASC') do |user|
  json.(user, :id, :name)
end

I want to sort users alphabetically so I tried .order('name ASC') but it doesn't work. Please help me find where I am going wrong.

Then I tried

@users.sort_by(&:name) it works but it is not case insensitive. It sorts the users as below.

Abby
James
William
bob
jack
rob

1 Answer 1

1

If you want to sort case insensitively you can do this.

@users.order("lower(name)")

or in Ruby

@users.sort_by! { |u| u.name.downcase }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.