4

I have two questions.

  1. Is there a way to sort collection from ActiveRecord or Thinking Sphinx without re-select everything from database/sphinx? ie.

    @models = Model.where("foo = :foo", {:foo => params[:foo]}) models.some_code_or_method_which_will_resort_everything

  2. Is this game worth a candle(sorting array/collection without fetching again)? I wonder which option is better for performance.

Thanks in advance.

Edit

So, this game is worth a candle when you:

  • don't use pagination(you must have all records)
  • refresh page div with ajax(don't fetch everything again with calling index or show action)

Art for art's sake..

nothing-special-here
  • 11,230
  • 13
  • 64
  • 94

1 Answers1

5

I'm pretty much sure 'sort' should work:

  @models.sort! { |a,b| a.foo <=> b.foo }

Answering the second question, yes, it does. It pretty much depends on the amount of objects in your collection, but generally it is usually a performance win

Vlad Khomich
  • 5,820
  • 1
  • 27
  • 39
  • 1
    Can you not sort as part of the database/Sphinx call? That way, the DB/Sphinx does all the hard work for you (and is likely faster than your Ruby code). It also means that paginated search results will be sorted _then_ paginated. – pat Aug 11 '11 at 12:51
  • initially, db will sort faster. But in case you need the same dataset but ordered differently - you'll need one more query, which is both db and ruby orm. This is definitely slower than just a ruby sort – Vlad Khomich Aug 11 '11 at 13:14