I have to build an array through both activerecord and also through a ferret index. I was hoping to find a way of sorting the array as i combine the two so def advanced_search(search_text, store, format, sortby) # find items in ferret index items = Item.find_by_contents(search_text) # and now find all the items from a certain store or category items_from_sql = Item.find_by_sql("SELECT * .....") @list = [] items.each do |i| @list << i if items_from_sql.include?(i) end #now i need to sort @list somehow with sortby which would equal "title" or "price" ?????? ?????? end Any suggestions on helping me rethink this method -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060531/2d325b8e/attachment.html
On 6/1/06, Jake Smith <rndholesqpeg@gmail.com> wrote:> I have to build an array through both activerecord and also through a ferret > index. > > I was hoping to find a way of sorting the array as i combine the two so > > def advanced_search(search_text, store, format, sortby) > > # find items in ferret index > items = Item.find_by_contents(search_text) > > # and now find all the items from a certain store or category > items_from_sql = Item.find_by_sql("SELECT * .....") > > @list = [] > items.each do |i| > @list << i if items_from_sql.include?(i) > end > > #now i need to sort @list somehow with sortby which would equal "title" or > "price" > ?????? > ?????? > > endHi Jake, I''d do the whole thing through Ferret; items = Item.find_by_contents(search_text, :sort => ["title", "price"]) I''m not sure what your SQL query is but I''m pretty sure it could be handled in Ferret. Let''s say you wanted to get all times with the subject of "Ruby" or "Rails". Instead of; items_from_sql = Item.find_by_sql("SELECT * items WHERE subject ''Ruby'' OR subject = ''Rails''") You''d do this; items = Item.find_by_contents(search_text + " +subject:(Ruby Rails)", :sort => ["title", "price"]) Or even better if speed is important, you could use a QueryFilter; # first you''d create a FILTER constant # NOTE: since we are creating the query directly we need to lowercase the # terms which would usually be done by the QueryParser bq = BooleanQuery.new() bq.add_query(TermQuery.new(Term.new("subject", "ruby"), BooleanClause::Occur::Should) bq.add_query(TermQuery.new(Term.new("subject", "rails"), BooleanClause::Occur::Should) FILTER = QueryFilter.new(bq) def advanced_search(search_text, store, format, sortby) items = Item.find_by_contents(search_text, :filter => FILTER, :sort => ["title", "price"]) #... end Hope that helps. Of course sorting arrays is easy to. @list.sort! do |a, b| cmp = (a.title <=> b.title) if cmp == 0 cmp = (a.price <=> b.price) end next cmp end Cheers, Dave
@list.sort! do |a, b| cmp = (a.title <=> b.title) if cmp == 0 cmp = (a.price <=> b.price) end next cmp end One question though, if .title or .price are being passed in as a string, how do i use that? # i know this wont work, just trying to help explain def sort_objects(objects,sortyby) cmp = (a.sortby <=> b.sortby if cmp == 0 cmp = (a.sortby <=> b.sortby) end next cmp end Thanks so much On 5/31/06, David Balmain <dbalmain.ml@gmail.com> wrote:> > On 6/1/06, Jake Smith <rndholesqpeg@gmail.com> wrote: > > I have to build an array through both activerecord and also through a > ferret > > index. > > > > I was hoping to find a way of sorting the array as i combine the two so > > > > def advanced_search(search_text, store, format, sortby) > > > > # find items in ferret index > > items = Item.find_by_contents(search_text) > > > > # and now find all the items from a certain store or category > > items_from_sql = Item.find_by_sql("SELECT * .....") > > > > @list = [] > > items.each do |i| > > @list << i if items_from_sql.include?(i) > > end > > > > #now i need to sort @list somehow with sortby which would equal "title" > or > > "price" > > ?????? > > ?????? > > > > end > > Hi Jake, > > I''d do the whole thing through Ferret; > > items = Item.find_by_contents(search_text, :sort => ["title", > "price"]) > > I''m not sure what your SQL query is but I''m pretty sure it could be > handled in Ferret. Let''s say you wanted to get all times with the > subject of "Ruby" or "Rails". Instead of; > > items_from_sql = Item.find_by_sql("SELECT * items WHERE subject > ''Ruby'' OR subject = ''Rails''") > > You''d do this; > > items = Item.find_by_contents(search_text + " +subject:(Ruby Rails)", > :sort => ["title", "price"]) > > Or even better if speed is important, you could use a QueryFilter; > > # first you''d create a FILTER constant > # NOTE: since we are creating the query directly we need to lowercase > the > # terms which would usually be done by the QueryParser > bq = BooleanQuery.new() > bq.add_query(TermQuery.new(Term.new("subject", "ruby"), > BooleanClause::Occur::Should) > bq.add_query(TermQuery.new(Term.new("subject", "rails"), > BooleanClause::Occur::Should) > FILTER = QueryFilter.new(bq) > > def advanced_search(search_text, store, format, sortby) > > items = Item.find_by_contents(search_text, > :filter => FILTER, > :sort => ["title", "price"]) > #... > end > > Hope that helps. Of course sorting arrays is easy to. > > @list.sort! do |a, b| > cmp = (a.title <=> b.title) > if cmp == 0 > cmp = (a.price <=> b.price) > end > next cmp > end > > Cheers, > Dave > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060601/b7439ebb/attachment.html
If sortby is an array of strings; def sort_objects(objects, sort_by) objects.sort! do |a,b| sort_by.each do |field| cmp = a.send(field) <=> b.send(field) break if cmp != 0 end next cmp end end Something like this anyway. I didn''t bother testing it. On 6/1/06, Jake Smith <rndholesqpeg@gmail.com> wrote:> @list.sort! do |a, b| > cmp = (a.title <=> b.title) > if cmp == 0 > cmp = (a.price <=> b.price) > end > next cmp > end > > One question though, if .title or .price are being passed in as a string, > how do i use that? > > # i know this wont work, just trying to help explain > def sort_objects(objects,sortyby) > cmp = (a.sortby <=> b.sortby > if cmp == 0 > cmp = (a.sortby <=> b.sortby) > end > next cmp > end > > > Thanks so much > > > On 5/31/06, David Balmain <dbalmain.ml@gmail.com> wrote: > > > On 6/1/06, Jake Smith <rndholesqpeg@gmail.com > wrote: > > I have to build an array through both activerecord and also through a > ferret > > index. > > > > I was hoping to find a way of sorting the array as i combine the two so > > > > def advanced_search(search_text, store, format, sortby) > > > > # find items in ferret index > > items = Item.find_by_contents(search_text) > > > > # and now find all the items from a certain store or category > > items_from_sql = Item.find_by_sql("SELECT * .....") > > > > @list = [] > > items.each do |i| > > @list << i if items_from_sql.include?(i) > > end > > > > #now i need to sort @list somehow with sortby which would equal "title" or > > "price" > > ?????? > > ?????? > > > > end > > Hi Jake, > > I''d do the whole thing through Ferret; > > items = Item.find_by_contents(search_text, :sort => ["title", "price"]) > > I''m not sure what your SQL query is but I''m pretty sure it could be > handled in Ferret. Let''s say you wanted to get all times with the > subject of "Ruby" or "Rails". Instead of; > > items_from_sql = Item.find_by_sql("SELECT * items WHERE subject > ''Ruby'' OR subject = ''Rails''") > > You''d do this; > > items = Item.find_by_contents(search_text + " +subject:(Ruby Rails)", > :sort => ["title", "price"]) > > Or even better if speed is important, you could use a QueryFilter; > > # first you''d create a FILTER constant > # NOTE: since we are creating the query directly we need to lowercase > the > # terms which would usually be done by the QueryParser > bq = BooleanQuery.new() > bq.add_query(TermQuery.new(Term.new("subject", "ruby"), > BooleanClause::Occur::Should) > bq.add_query(TermQuery.new(Term.new("subject", "rails"), > BooleanClause::Occur::Should) > FILTER = QueryFilter.new(bq) > > def advanced_search(search_text, store, format, sortby) > > items = Item.find_by_contents(search_text, > :filter => FILTER, > :sort => ["title", "price"]) > #... > end > > Hope that helps. Of course sorting arrays is easy to. > > @list.sort ! do |a, b| > cmp = (a.title <=> b.title) > if cmp == 0 > cmp = (a.price <=> b.price) > end > next cmp > end > > Cheers, > Dave > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Thanks so much jake On 6/1/06, David Balmain <dbalmain.ml@gmail.com> wrote:> > If sortby is an array of strings; > > def sort_objects(objects, sort_by) > objects.sort! do |a,b| > sort_by.each do |field| > cmp = a.send(field) <=> b.send(field) > break if cmp != 0 > end > next cmp > end > end > > Something like this anyway. I didn''t bother testing it. > > On 6/1/06, Jake Smith <rndholesqpeg@gmail.com> wrote: > > @list.sort! do |a, b| > > cmp = (a.title <=> b.title) > > if cmp == 0 > > cmp = (a.price <=> b.price) > > end > > next cmp > > end > > > > One question though, if .title or .price are being passed in as a > string, > > how do i use that? > > > > # i know this wont work, just trying to help explain > > def sort_objects(objects,sortyby) > > cmp = (a.sortby <=> b.sortby > > if cmp == 0 > > cmp = (a.sortby <=> b.sortby) > > end > > next cmp > > end > > > > > > Thanks so much > > > > > > On 5/31/06, David Balmain <dbalmain.ml@gmail.com> wrote: > > > > > On 6/1/06, Jake Smith <rndholesqpeg@gmail.com > wrote: > > > I have to build an array through both activerecord and also through a > > ferret > > > index. > > > > > > I was hoping to find a way of sorting the array as i combine the two > so > > > > > > def advanced_search(search_text, store, format, sortby) > > > > > > # find items in ferret index > > > items = Item.find_by_contents(search_text) > > > > > > # and now find all the items from a certain store or category > > > items_from_sql = Item.find_by_sql("SELECT * .....") > > > > > > @list = [] > > > items.each do |i| > > > @list << i if items_from_sql.include?(i) > > > end > > > > > > #now i need to sort @list somehow with sortby which would equal > "title" or > > > "price" > > > ?????? > > > ?????? > > > > > > end > > > > Hi Jake, > > > > I''d do the whole thing through Ferret; > > > > items = Item.find_by_contents(search_text, :sort => ["title", > "price"]) > > > > I''m not sure what your SQL query is but I''m pretty sure it could be > > handled in Ferret. Let''s say you wanted to get all times with the > > subject of "Ruby" or "Rails". Instead of; > > > > items_from_sql = Item.find_by_sql("SELECT * items WHERE subject > > ''Ruby'' OR subject = ''Rails''") > > > > You''d do this; > > > > items = Item.find_by_contents(search_text + " +subject:(Ruby > Rails)", > > :sort => ["title", "price"]) > > > > Or even better if speed is important, you could use a QueryFilter; > > > > # first you''d create a FILTER constant > > # NOTE: since we are creating the query directly we need to > lowercase > > the > > # terms which would usually be done by the QueryParser > > bq = BooleanQuery.new() > > bq.add_query(TermQuery.new(Term.new("subject", "ruby"), > > BooleanClause::Occur::Should) > > bq.add_query(TermQuery.new(Term.new("subject", "rails"), > > BooleanClause::Occur::Should) > > FILTER = QueryFilter.new(bq) > > > > def advanced_search(search_text, store, format, sortby) > > > > items = Item.find_by_contents(search_text, > > :filter => FILTER, > > :sort => ["title", "price"]) > > #... > > end > > > > Hope that helps. Of course sorting arrays is easy to. > > > > @list.sort ! do |a, b| > > cmp = (a.title <=> b.title) > > if cmp == 0 > > cmp = (a.price <=> b.price) > > end > > next cmp > > end > > > > Cheers, > > Dave > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060601/1a1490a0/attachment.html
dblack@wobblini.net
2006-Jun-01 12:11 UTC
[Rails] Help with sorting arrays with objects in it
Hi -- On Thu, 1 Jun 2006, Jake Smith wrote:> @list.sort! do |a, b| > cmp = (a.title <=> b.title) > if cmp == 0 > cmp = (a.price <=> b.price) > end > next cmp > end > > One question though, if .title or .price are being passed in as a string, > how do i use that? > > # i know this wont work, just trying to help explain > def sort_objects(objects,sortyby) > cmp = (a.sortby <=> b.sortby > if cmp == 0 > cmp = (a.sortby <=> b.sortby) > end > next cmp > endDoes this help? def sort_objects(objects,field) objects.sort_by {|obj| obj.send(field) } end David> > Thanks so much > > On 5/31/06, David Balmain <dbalmain.ml@gmail.com> wrote: >> >> On 6/1/06, Jake Smith <rndholesqpeg@gmail.com> wrote: >> > I have to build an array through both activerecord and also through a >> ferret >> > index. >> > >> > I was hoping to find a way of sorting the array as i combine the two so >> > >> > def advanced_search(search_text, store, format, sortby) >> > >> > # find items in ferret index >> > items = Item.find_by_contents(search_text) >> > >> > # and now find all the items from a certain store or category >> > items_from_sql = Item.find_by_sql("SELECT * .....") >> > >> > @list = [] >> > items.each do |i| >> > @list << i if items_from_sql.include?(i) >> > end >> > >> > #now i need to sort @list somehow with sortby which would equal "title" >> or >> > "price" >> > ?????? >> > ?????? >> > >> > end >> >> Hi Jake, >> >> I''d do the whole thing through Ferret; >> >> items = Item.find_by_contents(search_text, :sort => ["title", >> "price"]) >> >> I''m not sure what your SQL query is but I''m pretty sure it could be >> handled in Ferret. Let''s say you wanted to get all times with the >> subject of "Ruby" or "Rails". Instead of; >> >> items_from_sql = Item.find_by_sql("SELECT * items WHERE subject >> ''Ruby'' OR subject = ''Rails''") >> >> You''d do this; >> >> items = Item.find_by_contents(search_text + " +subject:(Ruby Rails)", >> :sort => ["title", "price"]) >> >> Or even better if speed is important, you could use a QueryFilter; >> >> # first you''d create a FILTER constant >> # NOTE: since we are creating the query directly we need to lowercase >> the >> # terms which would usually be done by the QueryParser >> bq = BooleanQuery.new() >> bq.add_query(TermQuery.new(Term.new("subject", "ruby"), >> BooleanClause::Occur::Should) >> bq.add_query(TermQuery.new(Term.new("subject", "rails"), >> BooleanClause::Occur::Should) >> FILTER = QueryFilter.new(bq) >> >> def advanced_search(search_text, store, format, sortby) >> >> items = Item.find_by_contents(search_text, >> :filter => FILTER, >> :sort => ["title", "price"]) >> #... >> end >> >> Hope that helps. Of course sorting arrays is easy to. >> >> @list.sort! do |a, b| >> cmp = (a.title <=> b.title) >> if cmp == 0 >> cmp = (a.price <=> b.price) >> end >> next cmp >> end >> >> Cheers, >> Dave >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >-- David A. Black (dblack@wobblini.net) * Ruby Power and Light, LLC (http://www.rubypowerandlight.com) > Ruby and Rails consultancy and training * Author of "Ruby for Rails" from Manning Publications! > http://www.manning.com/black