如何按属性订购对象?

时间:2015-12-26 11:13:16

标签: sorting ruby-on-rails-4 clone dup

我现在面对这个问题一段时间了,请仔细研究。

object.inspect给了我这个输出

<RawMaterial id: nil, name: "Jam Button 9 mm Antique Silver", rate: 1.0, raw_material_wastage: 0.0, total_raw_material: 8.0, slug: nil, costing_id: nil, created_at: nil, updated_at: nil, inventory_item_id: 758, costing_wastage: 0.0, pick_from_order_sheet: false>

raise object.to_yaml给出了这个输出

-- !ruby/object:RawMaterial
raw_attributes:
  costing_id: 
  id: 
  name: Jam Button 9 mm Antique Silver
  rate: '1'
  raw_material_wastage: '0'
  total_raw_material: '8'
  slug: 
  created_at: 
  updated_at: 
  inventory_item_id: '758'
  costing_wastage: '0'
  pick_from_order_sheet: f
attributes: !ruby/object:ActiveRecord::AttributeSet
  attributes: !ruby/object:ActiveRecord::LazyAttributeHash
    types:
      id: &3 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer
        precision: 
        scale: 
        limit: 
        range: !ruby/range
          begin: -2147483648
          end: 2147483648
          excl: true
      name: &2 !ruby/object:ActiveRecord::Type::String
        precision: 
        scale: 
        limit: 255
      rate: &1 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Float
        precision: 
        scale: 
        limit: 
      raw_material_wastage: *1
      total_raw_material: *1
      slug: *2
      costing_id: *3
      created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: &4 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime
          precision: 
          scale: 
          limit: 
      updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *4
      inventory_item_id: *3
      costing_wastage: *1
      pick_from_order_sheet: &5 !ruby/object:ActiveRecord::Type::Boolean
        precision: 
        scale: 
        limit: 
    values:
      id: '70'
      name: Jam Button 9 mm Antique Silver
      rate: '1'
      raw_material_wastage: '0'
      total_raw_material: '8'
      slug: 
      costing_id: '34'
      created_at: '2015-06-10 09:12:13.721016'
      updated_at: '2015-06-10 09:12:14.075739'
      inventory_item_id: '758'
      costing_wastage: '0'
      pick_from_order_sheet: f
    additional_types: {}
    materialized: true
    delegate_hash:
      costing_id: !ruby/object:ActiveRecord::Attribute::FromUser
        name: costing_id
        value_before_type_cast: 
        type: *3
        value: 
      id: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: id
        value_before_type_cast: 
        type: *3
        value: 
      name: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: name
        value_before_type_cast: Jam Button 9 mm Antique Silver
        type: *2
        value: Jam Button 9 mm Antique Silver
      rate: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: rate
        value_before_type_cast: '1'
        type: *1
        value: 1.0
      raw_material_wastage: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: raw_material_wastage
        value_before_type_cast: '0'
        type: *1
        value: 0.0
      total_raw_material: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: total_raw_material
        value_before_type_cast: '8'
        type: *1
        value: 8.0
      slug: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: slug
        value_before_type_cast: 
        type: *2
        value: 
      created_at: !ruby/object:ActiveRecord::Attribute::FromUser
        name: created_at
        value_before_type_cast: 
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *4
        value: 
      updated_at: !ruby/object:ActiveRecord::Attribute::FromUser
        name: updated_at
        value_before_type_cast: 
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *4
        value: 
      inventory_item_id: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: inventory_item_id
        value_before_type_cast: '758'
        type: *3
        value: 758
      costing_wastage: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: costing_wastage
        value_before_type_cast: '0'
        type: *1
        value: 0.0
      pick_from_order_sheet: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: pick_from_order_sheet
        value_before_type_cast: f
        type: *5
        value: false
new_record: true

我想按照创建raise object.to_yaml时获得的created_at值进行排序,如何做到这一点?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您有RawMaterial类型的对象列表。您可以使用对象字段['attributes']['attributes']['values']['created_at']对列表进行排序,该字段对于每个对象和Enumerable.sort_by方法都是唯一的。

enumerable_of_raw_materials.sort_by { |raw_material|
   raw_material['attributes']['attributes']['values']['created_at']
}