# Help with sorting arrays with objects in it

**URL:** <https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143>\
**Category:** Rails\
**Created:** [May 31, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143 "2006-05-31T21:48:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [May 31, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/1 "2006-05-31T21:48:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [June 1, 2006, 12:58am UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/2 "2006-06-01T00:58:49Z")

</div>

On 6/1/06, Jake S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # and now find all the items from a certain store or category
> 
> ???
> 
> 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

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [June 1, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/3 "2006-06-01T12:47:47Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [June 1, 2006, 1:33pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/4 "2006-06-01T13:33:56Z")

</div>

Thanks so much

jake

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [June 1, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/5 "2006-06-01T13:00:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [June 1, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/help-with-sorting-arrays-with-objects-in-it/62143/6 "2006-06-01T14:13:49Z")

</div>

Hi –

On Thu, 1 Jun 2006, Jake S. wrote:

> # 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

Does this help?

def sort\_objects(objects,field)  
objects.sort\_by {|obj| obj.send(field) }  
end

David

> > > I was hoping to find a way of sorting the array as i combine the two so  
> > > items.each do |i|
> > 
> > ```
> > # first you'd create a FILTER constant
> > def advanced_search(search_text, store, format, sortby)
> > cmp = (a.title <=> b.title)
> > 
> > ```
> > 
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
David A. Black (removed\_email\_address@domain.invalid)

- Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

> Ruby and Rails consultancy and training

- Author of “Ruby for Rails” from Manning Publications!

> [Ruby for Rails](http://www.manning.com/black)
