Ruby on rails 如何在Ruby中对JSON对象的嵌套数组进行排序?
早上好!我在Rails中有一个棘手的数据集要处理。它是一个JSON对象数组,每个对象有4个属性。示例如下:Ruby on rails 如何在Ruby中对JSON对象的嵌套数组进行排序?,ruby-on-rails,json,ruby,Ruby On Rails,Json,Ruby,早上好!我在Rails中有一个棘手的数据集要处理。它是一个JSON对象数组,每个对象有4个属性。示例如下: [ [ { "column_name": "ID", "column_id": "item_attributes#id", "column_value": "25295119-7113-42fe-90d5-394c7a841b31", "column_type": "st
[
[
{
"column_name": "ID",
"column_id": "item_attributes#id",
"column_value": "25295119-7113-42fe-90d5-394c7a841b31",
"column_type": "string"
},
{
"column_name": "Name",
"column_id": "item_attributes#name",
"column_value": "Webform submission",
"column_type": "string"
},
{
"column_name": "Current owner",
"column_id": "item_attributes#current_owner",
"column_value": "Joe Bloggs",
"column_type": "string"
},
{
"column_name": "State",
"column_id": "item_attributes#state",
"column_value": "New Task",
"column_type": "string"
},
{
"column_name": "Status",
"column_id": "item_attributes#status",
"column_value": "In progress",
"column_type": "string"
},
{
"column_name": "Created at",
"column_id": "item_attributes#created_at",
"column_value": "07/09/2018, 10:47 am",
"column_type": "datetime"
},
{
"column_name": "Updated at",
"column_id": "item_attributes#updated_at",
"column_value": "07/09/2018, 10:47 am",
"column_type": "datetime"
},
{
"column_name": "Task reviewer",
"column_id": "item_participant_attributes#90a76abe-028f-45bc-8390-433bc39ba02e",
"column_value": "Jane Bloggs",
"column_type": "string"
},
{
"column_name": "Task owner",
"column_id": "item_participant_attributes#5bce3a02-07bf-4353-9f3d-2cc60f787584",
"column_value": "Jack blogs",
"column_type": "string"
},
{
"column_name": "Make a list of your weekly tasks. Update status of this object to complete when it's complete.",
"column_id": "element_attributes#533e26d8-c313-4d94-ab99-37263b0aa4ee",
"column_value": null,
"column_type": "string"
},
{
"column_name": "Set target end date (choose Friday for end of this week)",
"column_id": "element_attributes#6eeddd67-e1de-40c3-b220-197ddf554241",
"column_value": null,
"column_type": "datetime"
},
{
"column_name": "Reviewer comments (optional)",
"column_id": "element_attributes#3923358c-85f0-444b-9287-4738d9148eb9",
"column_value": null,
"column_type": "string"
}
],
[...]
]
我有一个名为ReportTemplateColumn
的模型,该模型定义了每个项目所需的sort\u优先级
和sort\u方向
,以及相关的column\u id
我需要遍历ReportTemplateColumns
,并将它们的排序方向按优先级顺序应用于上述数据集
例如,我知道我需要按照column\u id=item\u attributes\name
的顺序对数据集重新排序。数组数组需要根据列\u值
值更改顺序
我的尝试
def set_sorted_data(data)
@new_data = data
self.report_template.report_template_columns.where('sort_priority != ?', 1000).order(:sort_priority).each do |report_template_column|
@new_data = @new_data.sort_by { |x| x[0]["column_value"] }
end
return @new_data
end
我认为,
.sort_by{x|x[0][“column_value”]}
是主要问题,例如,如果您得到这样的结果
f = [[{'a' => 4}, {'a' => 2}]]
# => [[{"a"=>4}, {"a"=>2}]]
f.sort_by{|d| d[0]['a']}
# => [[{"a"=>4}, {"a"=>2}]], won't change anything
f.first.sort_by{|d| d['a'] } # first represent the [0] here
# => [{"a"=>2}, {"a"=>4}], This gonna work
使用您的方法时出现了一些错误,即您正在对外部数组进行排序,当它像这样{x | x[0][“column_value”]}
时,我相信ruby试图对(外部数组)
进行排序,方法是对其内部数组(这是一个)
进行排序,方法是对名为'column_value'
的某个属性进行排序,但是指定您对内部数组的操作将影响上面的示例中的内部数组,希望它能与您一起工作
您可以使用
排序依据代码>将其排序到位。下面是一个已工作的实现,可能这不是最佳的,但它可以工作:
def set_sorted_data(data)
@new_data = data
self.report_template.report_template_columns.where('sort_priority != ?', 1000).order(:sort_priority).reverse.each_with_index do |report_template_column|
@new_data[0].each_with_index do |item_row_datum, index|
if item_row_datum[:column_id] == report_template_column.column_id
@index = index
end
end
if report_template_column.column_type == 'number'
@new_data.sort_by! { |item_row_datum| item_row_datum[@index][:column_value].to_i }
elsif report_template_column.column_type == 'datetime'
@new_data.sort_by! { |item_row_datum| item_row_datum[@index][:column_value].to_date }
else
@new_data.sort_by! { |item_row_datum| item_row_datum[@index][:column_value].to_s }
end
if report_template_column.sort_direction == 'desc'
@new_data = @new_data.reverse
end
end
return @new_data
end
谢谢你,它为我指明了正确的方向。我的实现现在也作为一个答案发布。