Sort array of arrays that is a value of a hash

emico7

I saw several posts on sorting an array that is a value of a hash, but I'm trying to sort an array of arrays that is a value of a hash. I have a hash that looks like this:

h = { 
      "pets"=>[["dog", 1], ["cat", 2]],
      "fruits"=>[["orange", 1], ["apple", 2]]
     }

I would like to sort array of arrays according to the first elements (string) of the arrays inside. So I want the result to be

{ 
  "pets"=>[["cat", 2], ["dog", 1]],
  "fruits"=>[["apple", 2], ["orange", 1]]
}

This is what I have right now:

h.map do |key, value|
  value.sort_by! { |x, y| x[0] <=> y[0] }
end

But this just returns the original array. What do I need to change? Thanks!

Rockster160

The one issue I see is that you are attempting to map your hash. That will always return an array.

In this case, you’ll get back something like this: [[["cat", 2], ["dog", 1]], [["apple", 2], ["orange", 1]]]

If you want to modify your hash in place, you can do:

h.each { |key, value| h[key] = value.sort_by { ... } }

Then h will be the new hash, sorted as you expect. Or you can set it to another variable:

new_hash = h.each { |key, value| value.sort_by! { ... } }

Next, it looks like you’ve confused sort and sort_by. (Easy to do) When you use sort, the two variables you pass in your block are assigned to the values being compared. (In your case, the arrays ["dog", 1] is one argument, and the other would be another array to compare it to, like ["cat", 2]) However, sort_by expects you to do the computing to determine where it’s place is in the new order. Normally sort_by only accepts a single argument, but because you’re working with arrays (and because of the way Ruby handles tuples) when you tell sort_by to accept 2 arguments (x and y) those are set to the first and second values of your array. (x == "dog", y == 1)

So you’ve got 2 options here: Keep your block the same and switch your enumerable method to sort, or switch to sort_by and change your block:

sort:

h.each { |key, value| h[key] = value.sort! { |x, y| x[0] <=> y[0] } }

sort_by:

h.each { |key, value| h[key] = value.sort_by! { |x, y| y } } # In this case, `y` is the second value in your arrays, which is the integer

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Sort array of arrays by array value into hash in ruby

分類Dev

PHP array sort by value

分類Dev

ruby array of hash with same value

分類Dev

Sort List of Arrays by String Date Value

分類Dev

Combining parallel arrays into single object array and sort

分類Dev

Sort javascript object with array as value

分類Dev

Ruby array of arrays find by inner array value

分類Dev

Given a hash of arrays, how to create an array of hashes with each possible combo

分類Dev

How to sort mongoose populated array by a value?

分類Dev

order or sort array of objects based on a key value

分類Dev

Sort array of dictionaries based on key and value

分類Dev

jq sort using the value of a nested array element

分類Dev

PHP sort array of associative arrays by element size / string size

分類Dev

Sort Three Arrays by ArrayList Index (Row) on a Single Array

分類Dev

PHP Arrays, referencing an earlier value in same array

分類Dev

Ruby sort hash of hashes

分類Dev

how to sort multi dimensional array in PHP using array value?

分類Dev

Sort [key => value] array by another array of keys with same index

分類Dev

using Arrays.sort between Integer.MIN_VALUE, Integer.MAX_VALUE values with Comparator

分類Dev

NSArray sort arrays

分類Dev

How to sort an array of objects by property value in Angular with custom pipe?

分類Dev

How sort array by int part of variables value in javascript?

分類Dev

How do sort array with key, value in Angular JS?

分類Dev

How do sort array with key, value in Angular JS?

分類Dev

How to map an Array of arrays to object like property and value?

分類Dev

Uitableview Sort with Hash Symbol in sectionIndexTitles

分類Dev

add a key value pair to hash in first array depending on the substring from second array

分類Dev

How to sort 1 array in Swift / Xcode and reorder multiple other arrays by the same keys changes

分類Dev

Updating Hash value in Ruby

Related 関連記事

  1. 1

    Sort array of arrays by array value into hash in ruby

  2. 2

    PHP array sort by value

  3. 3

    ruby array of hash with same value

  4. 4

    Sort List of Arrays by String Date Value

  5. 5

    Combining parallel arrays into single object array and sort

  6. 6

    Sort javascript object with array as value

  7. 7

    Ruby array of arrays find by inner array value

  8. 8

    Given a hash of arrays, how to create an array of hashes with each possible combo

  9. 9

    How to sort mongoose populated array by a value?

  10. 10

    order or sort array of objects based on a key value

  11. 11

    Sort array of dictionaries based on key and value

  12. 12

    jq sort using the value of a nested array element

  13. 13

    PHP sort array of associative arrays by element size / string size

  14. 14

    Sort Three Arrays by ArrayList Index (Row) on a Single Array

  15. 15

    PHP Arrays, referencing an earlier value in same array

  16. 16

    Ruby sort hash of hashes

  17. 17

    how to sort multi dimensional array in PHP using array value?

  18. 18

    Sort [key => value] array by another array of keys with same index

  19. 19

    using Arrays.sort between Integer.MIN_VALUE, Integer.MAX_VALUE values with Comparator

  20. 20

    NSArray sort arrays

  21. 21

    How to sort an array of objects by property value in Angular with custom pipe?

  22. 22

    How sort array by int part of variables value in javascript?

  23. 23

    How do sort array with key, value in Angular JS?

  24. 24

    How do sort array with key, value in Angular JS?

  25. 25

    How to map an Array of arrays to object like property and value?

  26. 26

    Uitableview Sort with Hash Symbol in sectionIndexTitles

  27. 27

    add a key value pair to hash in first array depending on the substring from second array

  28. 28

    How to sort 1 array in Swift / Xcode and reorder multiple other arrays by the same keys changes

  29. 29

    Updating Hash value in Ruby

ホットタグ

アーカイブ