Rails 有条件地按自定义顺序循环哈希
我正在研究ROR Web应用程序。
我必须根据哈希的值按自定义顺序循环一系列哈希。
在此示例中,“待处理”状态需要首先出现在列表中。
是否可以自定义每个循环中数组的顺序?
<% example_statuses = [{ :status => "Active", :job_count => 0 }, { :status => "Pending", :job_count => 1 }, { :status => "Complete", :job_count => 3 }] %>
<% example_statuses.each do |es| %>
<h3><%= es[:status] %></h3>
<% end %>
更新: 我有一个简单的工作示例,其中涉及删除和准备待处理的状态放回阵列中。但是,有没有一种方法可以创建自定义订单,而不是一个人手动替换值?
<% example_statuses = [{ :status => "Active", :job_count => 0 }, { :status => "Pending", :job_count => 1 }, { :status => "Complete", :job_count => 3 }] %>
<%= pending = example_statuses.find { |x| x[:status] == "Pending"} %>
<% example_statuses.delete(pending) %>
<% example_statuses.prepend(pending) %>
<% example_statuses.each do |es| %>
<h3><%= es[:status] %></h3>
<% end %>
I'm working on a RoR web application.
I have to loop over an array of hashes in a custom order based on the value of the hash.
In this example, the "Pending" status needs to appear first in the list.
Is it possible to customize the order of the array in the each loop?
<% example_statuses = [{ :status => "Active", :job_count => 0 }, { :status => "Pending", :job_count => 1 }, { :status => "Complete", :job_count => 3 }] %>
<% example_statuses.each do |es| %>
<h3><%= es[:status] %></h3>
<% end %>
UPDATE:
I have a simple working example that involves removing and prepending the pending status hash back into the array. HOWEVER is there a way to create a custom order instead of manually replacing values one by one?
<% example_statuses = [{ :status => "Active", :job_count => 0 }, { :status => "Pending", :job_count => 1 }, { :status => "Complete", :job_count => 3 }] %>
<%= pending = example_statuses.find { |x| x[:status] == "Pending"} %>
<% example_statuses.delete(pending) %>
<% example_statuses.prepend(pending) %>
<% example_statuses.each do |es| %>
<h3><%= es[:status] %></h3>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有两个重要的概念:
数组#索引
在数组中找到一个元素。&lt; =&gt;
,这是array#sort
工作的方式。您可以调查在您的情况下在您的情况下解决方案是:
There are two important concepts here:
Array#index
to find where in an array an element is found.<=>
which is howArray#sort
works. You can investigate it hereIn your case solution would be:
您可以尝试使用像这样的单行
example_statuses.sort_by{|x| x[:状态] == '待处理' ? '' : x[:状态] }
you can try it with single line like this
example_statuses.sort_by{|x| x[:status] == 'Pending' ? '' : x[:status] }
据我了解,给您一个数组
order
,可以重新排序以相等,并且您希望对
example_statuses
进行排序以产生数组排序
您不需要实际对
example_statues
进行排序,它具有O(n*log(n)
)的计算复杂性,其中n
等于example_statuses
中的元素数。相反,执行以下内容,其具有接近O(
n
)的计算复杂性。假设案例1:
g [:status]!= h [:状态]
对于所有不同元素的对example_statuses
在这种情况下,计算
hash#values_at 被认为是以下内容。
我早些时候声称计算复杂性是“几乎” O(
n
)。构建哈希example_statuess.each_with_object({}){| g,h | h [g [:status]] = g}
是o(n
),但每个元素的密钥查找或order> order
仅是“几乎” o( 1)。是恒定的时间,复杂性将为o(n
)。案例2:
g [:状态] == H [:状态]
至少一对不同的元素g
和h
example_statuses
当然可能不允许使用这种情况,但该问题尚未清楚。
假设
顺序
与以前一样,但是example_statuses
如下。请注意,
example_statuses [0] [:状态]
和example_statuses [3] [:status]
均等“ active” 。我们只需要对情况1计算(不影响计算复杂性)进行稍作修改。
values_at
的接收器被认为等于以下内容。请参阅 hash :: new 的形式那需要一个障碍,没有参数。当
h
没有键[g [:status]
这导致h [g [:status]]
分配给空的在执行g [:status]&lt;&lt;执行之前的数组; G
。As I understand you are given an array
order
that can be reordered to equaland that you wish to sort
example_statuses
to produce an arraysorted
such thatYou do not need to actually sort
example_statues
, which would have a computational complexity of O(n*log(n)
) wheren
equals the number of elements inexample_statuses
.Instead, do the following, which has a computational complexity close to O(
n
). SupposeCase 1:
g[:status] != h[:status]
for all pairs of distinct elementsg
andh
ofexample_statuses
In this case compute
The receiver of Hash#values_at is seen to be the following.
I earlier claimed that the computational complexity was "almost" O(
n
). Building the hashexample_statuses.each_with_object({}) { |g,h| h[g[:status]] = g }
is O(n
) but the key lookup for each element ororder
is only "almost" O(1). Were is a constant time the complexity would be O(n
).Case 2:
g[:status] == h[:status]
for at least one pair of distinct elementsg
andh
ofexample_statuses
Of course this case may not be permitted but that has not been made clear by the question.
Suppose
order
is as before butexample_statuses
is as follows.Note that
example_statuses[0][:status]
andexample_statuses[3][:status]
both equal"Active"
.We need just a slight modification of the Case 1 calculation (which does not affect computational complexity).
The receivers of
values_at
is seen to equal the following.See the form of Hash::new that takes a block and no argument. When
h
does not have a key[g[:status]
this causesh[g[:status]]
to be assigned to an empty array before executingg[:status]<< g
.