Sorting a mixed array of hashes and strings
2014-09-06 16:10
232 查看
What is the syntax for sorting an array alphabetically by the key of a hash or by a string? I would like to sort the following:
to give the following:
Not clear what to do when a hash includes more than one key. Considering always the first key in a hash:
['bob', 'apple', 'sandwich', {'lasagne' => 'munch'}, 'tangoed']
to give the following:
['apple', 'bob', {'lasagne' => 'munch'}, 'sandwich', 'tangoed']
Not clear what to do when a hash includes more than one key. Considering always the first key in a hash:
['bob', 'apple', 'sandwich', {'lasagne' => 'munch'}, 'tangoed'] .sort_by{|e| [*e].flatten.first} #=> ["apple", "bob", {"lasagne"=>"munch"}, "sandwich", "tangoed"]
相关文章推荐
- Call C# Code from C++ and read an array of struct which contains strings
- Sort an Array Mixed With Integers and Strings - Ruby
- ruby merge array of hashes based on the key and order it by number of key/value pair
- Array of Strings to Convert to Mixed Array
- Ruby: Sorting an array of strings, in alphabetical order, that includes some arrays of strings
- Group and sum array of hashes by date
- Group and sum array of hashes by date
- Sort array of strings
- note : 1.8 Allocating and Making Use of Strings
- 保存自定义对象的数组 save and restore an array of custom objects
- pointer to ref of an array, and to the func that return a ref of an array
- LeetCode238 Product of Array Except Self(java and python solution)
- Rearrange an array of positive and negative integers
- “System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 错误
- Write a function to find the longest common common string amongst an array of strings.
- Sorting an array of hash in Ruby
- Creating one array of strings in c fails ,why?
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- Array.forEach() and Using square brackets to obtain the value of a property
- Given two strings s and t, write a function to determine if t is an anagram of s.