我如何在ruby中对这些键进行排序?

时间:2021-08-27 07:43:25

So I've got this Hash. It looks like this:

我得到了这个哈希。它看起来像这样:

{
  'arg0' => '126150656000',
  'arg1' => 'Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz',
  'arg2' => '2790',
  'arg3' => '3276320768',
  'arg4' => '8467496960',
  'arg5' => 'Windows 7',
  'arg6' => '6.1',
  'arg7' => 'amd64',
  'arg8' => '2',
  'arg9' => '1920',
  'arg10' => '1200',
  'arg11' => '32',
}

The Hash needs to be transformed into an array of positional args based on the 'argN' position of the key. Like so.

根据关键字的“argN”位置,需要将散列转换为位置参数数组。像这样。

[
  '126150656000',
  'Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz',
  '2790',
  '3276320768',
  '8467496960',
  'Windows 7',
  '6.1',
  'amd64',
  '2',
  '1920',
  '1200',
  '32'
]

The goal here is that [0] == ['arg0'], [1] == ['arg1'], [N] == ['argN'].

这里的目标是,[0]= =[' arg0 '],[1]= =[' __arg1 '],[N]= =[' argN ']。

NOTE:

注意:

The keys can NOT be guaranteed to be in the correct order. For example the hash above may have 'arg9' "before" 'arg4'. Sorry for not making that clear.

不能保证键的顺序正确。例如,上面的散列可能在“arg4”之前有“arg9”。抱歉没说清楚。

3 个解决方案

#1


5  

 h.sort_by { | a, _ | a.gsub(/[^\d]/, '').to_i }.map(&:last)

#2


1  

Disorganizing your Hash first:

志在你散列:

h = {
  'arg1' => 'Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz',
  'arg3' => '3276320768',
  'arg4' => '8467496960',
  'arg7' => 'amd64',
  'arg5' => 'Windows 7',
  'arg2' => '2790',
  'arg6' => '6.1',
  'arg9' => '1920',
  'arg8' => '2',
  'arg0' => '126150656000',
  'arg10' => '1200',
  'arg11' => '32',
}

You can do this:

你可以这样做:

h.keys.sort.map{|k| h[k]}
# => ["126150656000", "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz", "1200",
#     "32", "2790", "3276320768", "8467496960", "Windows 7", "6.1",
#     "amd64", "2", "1920"]

Update: This is assuming that you want your keys sorted in standard sort order, which if they are literally 'arg0' through 'arg11', isn't what you expect. I'm guessing your actually keys are something more useful. If these are you actual keys, you might do:

更新:这是假设你想要你的键按标准的排序顺序排序,如果按字面意思“arg0”到“arg11”,这不是你想要的。我猜你的钥匙更有用。如果这些是你真正的钥匙,你可以这样做:

h.keys.sort_by{|s| s[3..-1].to_i}.map{|k| h[k]}
# => ["126150656000", "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz", "2790",
#     "3276320768", "8467496960", "Windows 7", "6.1", "amd64", "2", "1920",
#     "1200", "32"]

#3


0  

The answers provided are good if the hash contains keys that are unsorted. Based on your example, it looks like they are already ordered. If thats the case, then all you need to do is do {}.values to obtain the "ordered" array of the values.

如果散列包含未排序的键,则提供的答案是正确的。根据您的示例,看起来它们已经被排序了。如果是这样,那么您需要做的就是执行{}。获取值的“有序”数组。

#1


5  

 h.sort_by { | a, _ | a.gsub(/[^\d]/, '').to_i }.map(&:last)

#2


1  

Disorganizing your Hash first:

志在你散列:

h = {
  'arg1' => 'Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz',
  'arg3' => '3276320768',
  'arg4' => '8467496960',
  'arg7' => 'amd64',
  'arg5' => 'Windows 7',
  'arg2' => '2790',
  'arg6' => '6.1',
  'arg9' => '1920',
  'arg8' => '2',
  'arg0' => '126150656000',
  'arg10' => '1200',
  'arg11' => '32',
}

You can do this:

你可以这样做:

h.keys.sort.map{|k| h[k]}
# => ["126150656000", "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz", "1200",
#     "32", "2790", "3276320768", "8467496960", "Windows 7", "6.1",
#     "amd64", "2", "1920"]

Update: This is assuming that you want your keys sorted in standard sort order, which if they are literally 'arg0' through 'arg11', isn't what you expect. I'm guessing your actually keys are something more useful. If these are you actual keys, you might do:

更新:这是假设你想要你的键按标准的排序顺序排序,如果按字面意思“arg0”到“arg11”,这不是你想要的。我猜你的钥匙更有用。如果这些是你真正的钥匙,你可以这样做:

h.keys.sort_by{|s| s[3..-1].to_i}.map{|k| h[k]}
# => ["126150656000", "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz", "2790",
#     "3276320768", "8467496960", "Windows 7", "6.1", "amd64", "2", "1920",
#     "1200", "32"]

#3


0  

The answers provided are good if the hash contains keys that are unsorted. Based on your example, it looks like they are already ordered. If thats the case, then all you need to do is do {}.values to obtain the "ordered" array of the values.

如果散列包含未排序的键,则提供的答案是正确的。根据您的示例,看起来它们已经被排序了。如果是这样,那么您需要做的就是执行{}。获取值的“有序”数组。