简体   繁体   English

从 STRIPE API / Rails 中按字母顺序对数组中的哈希值进行排序

[英]Sort alphabetically hash's values in array from STRIPE API / Rails

In rails 6.1.4, I want to display the:nickname of a Product in STRIPE in DESC order.在 rails 6.1.4 中,我想以 DESC 顺序在 STRIPE 中显示产品的昵称。 I mean S, M, L on my VIEW page and not in a random way like Stripe give us.我的意思是我的 VIEW 页面上的 S、M、L,而不是像 Stripe 给我们的随机方式。 In their https://stripe.com/docs/api it seems we couldn't make the request on an easy way.在他们的https://stripe.com/docs/api中,我们似乎无法以简单的方式提出请求。

For this, I query @compositionsProduct = Stripe::Price.list(product: @composition.product) .为此,我查询@compositionsProduct = Stripe::Price.list(product: @composition.product) It returns me an Array of Hash in the data part:它在data部分返回一个 Hash 数组:

    => #<Stripe::ListObject:0x3ff733e41c08> JSON: {
  "object": "list",
  "data": [
    {"id":"price_price1","object":"price","active":true,"billing_scheme":"per_unit","created":1635150421,"currency":"eur","livemode":false,"lookup_key":null,"metadata":{},"nickname":"L","product":"prod_prod1","recurring":null,"tax_behavior":"unspecified","tiers_mode":null,"transform_quantity":null,"type":"one_time","unit_amount":4500,"unit_amount_decimal":"4500"},
    {"id":"price_price2","object":"price","active":true,"billing_scheme":"per_unit","created":1635150421,"currency":"eur","livemode":false,"lookup_key":null,"metadata":{},"nickname":"S","product":"prod_prod1","recurring":null,"tax_behavior":"unspecified","tiers_mode":null,"transform_quantity":null,"type":"one_time","unit_amount":2500,"unit_amount_decimal":"2500"},
    {"id":"price_price3","object":"price","active":true,"billing_scheme":"per_unit","created":1635150421,"currency":"eur","livemode":false,"lookup_key":null,"metadata":{},"nickname":"M","product":"prod_prod1","recurring":null,"tax_behavior":"unspecified","tiers_mode":null,"transform_quantity":null,"type":"one_time","unit_amount":3500,"unit_amount_decimal":"3500"}
  ],
  "has_more": false,
  "url": "/v1/prices"
}

I want to write something like this: Stripe::Price.list(product: @composition.product).sort_by([:nickname].reverse) but it obviously doesn't work.我想写这样的东西: Stripe::Price.list(product: @composition.product).sort_by([:nickname].reverse)但它显然不起作用。

I can iterate over these 3 lines, but I need to linked_to them on their Price page respectively.我可以遍历这 3 行,但我需要在他们的价格页面上分别linked_to到它们。

<% @compositionsProduct.map do |compoPrice| %>
  <%= link_to "#{compoPrice.nickname}", composition_path(compoPrice.id) %>
<% end %>

This is my farest point: @compositionsProduct.sort_by { |hash| hash[:nickname]}这是我最远的一点: @compositionsProduct.sort_by { |hash| hash[:nickname]} @compositionsProduct.sort_by { |hash| hash[:nickname]} negative sign doesn't work, neither .reverse @compositionsProduct.sort_by { |hash| hash[:nickname]}负号不起作用, .reverse也不起作用

What is the best way to sort_by alphabetically the values of a specific line in an array of hash?按字母顺序排序sort_by数组中特定行的值的最佳方法是什么?

@compositionsProduct.sort_by { |hash| hash[:nickname] }.reverse @compositionsProduct.sort_by { |hash| hash[:nickname] }.reverse works very well. @compositionsProduct.sort_by { |hash| hash[:nickname] }.reverse效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM