Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 163197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:31:31+00:00 2026-05-11T11:31:31+00:00

I have a little example Rails app called tickets, which views and edits fictional

  • 0

I have a little example Rails app called tickets, which views and edits fictional tickets sold to various customers. In tickets_controller.rb, inside def index, I have this standard line, generated by scaffolding:

@tickets = Ticket.find(:all) 

To sort the tickets by name, I have found two possible approaches. You can do it this way:

@tickets = Ticket.find(:all, :order => 'name') 

… or this way:

@tickets = Ticket.find(:all).sort!{|t1,t2|t1.name <=> t2.name} 

(Tip: Ruby documentation explains that sort! will modify the array that it is sorting, as opposed to sort alone, which returns the sorted array but leaves the original unchanged).

What strategy do you normally use? When might you use .sort! versus the :order => 'criteria' syntax?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:31:31+00:00Added an answer on May 11, 2026 at 11:31 am

    Use :order => 'criteria' for anything simple that can be done by the database (ie. basic alphabetical or chronological order). Chances are it’s a lot faster than letting your Ruby code do it, assuming you have the right indexes in place.

    The only time I could think you should use the sort method is if you have a complex attribute that’s calculated at run-time and not stored in the database, like a ‘trustworthiness value’ based off number of good/bad responses or something. In that case it’s better to use the sort method, but be aware that this will screw things up if you have pagination in place (each page will have ITS results in order, but the set of pages as a whole will be out of order).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently complete Hartl's Rails tutorial, and have devised a little app that
I have created a little example,please have a look, http://jsfiddle.net/bWTwL/ I want to have
I am writing some documentation and I have a little vocabulary problem: http://www.example.com/en/public/img/logo.gif is
I'm using ActionBarSherlock and have a little problem. In the Fragment Tabs example, there
Can anybody give me a little advice please? I have a string, for example
So, I'm writing a game engine. I have a little example (see http://qrcode.kaywa.com/img.php?s=8&d=http://rokonandroid.com/rd.apk ),
I have created a little example(below) on my attempt to safely stop multiple threads
I have just wrote a little example and i can't manage to make it
I have a Ruby on Rails app that has a scheduled job I execute
I have a Rails app with some basic models. The website displays data retrieved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.