Reputation: 42865
Language: Ruby
I have an array of Products. Each Product has an attribute called product_price_name.
[Product.product_price_name = "All Prices", Product.product_price_name = "$1 to $100" ]
I want to sort this array so the first result is "All Prices" and then the next options will be price ranges such as $1- $100, $100- $200
Upvotes: 0
Views: 158
Reputation: 27793
# some setup
Product = Struct.new(:product_price_name)
array = %w{$1-100 $200-1000 All_Prices $100-200}.collect{|each|Product.new(each)}
# the actual code
array = array.sort_by { |each| each.product_price_name }
array.unshift array.pop
array # => [#<struct Product product_price_name="All_Prices">, #<struct Product product_price_name="$1-100">, #<struct Product product_price_name="$100-200">, #<struct Product product_price_name="$200-1000">]
Gosh, one should never submit code that had never run! Mea culpa.
Upvotes: 4