How to sort hashes in an array by keys? - arrays

Eg:
arr = ["c", "e", "a", {"hello" => [1,2,3]}, {"bell" => [4,5,6]}]
Above variable refers to an array which contains hashes along with strings. I need to write a function to sort this array alphabetically while the hashes being sorted by keys. Ideally it has to return the following:
["a", {"bell" => [4,5,6]}, "c", "e", {"hello" => [1,2,3]}]

array.sort_by {|a| a.is_a?(Hash) ? a.keys.first : a }

Related

Compare each element in two arrays Swift

I have two arrays of strings. for example:
let arrayFirst: [String] = ["A", "A", "A", "A", "A"]
let arraySecond: [String] = ["A", "C", "A", "B", "A"]
I need to compare this two arrays each element in array and return for every sequence bool state.
For example here will be answer:
let resultArray: [Bool] = [true, false, true, false, true]
how to do it better?
You can consider using the zip function.
let resultArray = zip(arrayFirst, arraySecond).map {
return $0.0 == $0.1
}
This will work even you have arrays of different length as zip will ignore the additional elements of the longer array.

How to make my test pass a non mult-dimensional array to my React Test?

I have a utility method that I am writing a test for and I am trying to pass arguments to accurately portray usage and produce a non multi-dimensional array. Here is my code
const arrayInsert = (arr: any[], index: number, ...newItems) => [
...arr.slice(0, index),
...newItems,
...arr.slice(index)
];
export default arrayInsert;
As you see the last argument is a spread operator. And right now I am passing arguments to my test that produce a multi-dimensional array instead of adding to the array like this example:
arrayInsert(["A", "B", "F"], 1, "C", "D", "E");
// produces
["A", "C", "D", "E", "B", "F"]
So I want my test to be written to accurately portray the above example.
import arrayInsert from "./arrayInsert";
describe("arrayInsert", () => {
it("should return array containing an array of new items", () => {
const arr = ["A", "B", "F"];
const newItems = ["C", "D", "E"];
const index = 1;
const expected = ["A", ["C", "D", "E"], "B", "F"];
expect(arrayInsert(arr, index, newItems)).toEqual(expected);
});
});
How can I make the result one dimensional?

Swift 3 - Check if a string exist inside an array and sort it

I have this special array var myArray : [Array<String>] = [["a"],["b"],["c"]]
I want to detect if "a" is already inside myArray
and after i would like to sort my array alphabetically but i havn't found any function to do all these things for my array
To find if your [[String]] contains "a", you can use contains twice:
var myArray : [Array<String>] = [["a"],["b"],["c"]]
if myArray.contains(where: { $0.contains("a") }) {
print("a found")
}
To sort the inner arrays, apply map to the outer array and sort each element:
var myArray : [Array<String>] = [["c", "a"], ["e", "b"], ["d"]]
let sorted = myArray.map { $0.sorted() }
print(sorted)
[["a", "c"], ["b", "e"], ["d"]]

Merging hashes into arrays

I'm new in Ruby. I have two arrays of hashes
arr1 = [{"one"=> {"1"=> "a", "2" => "b"}, "two" => {"3" => "n", "5" => "h", "7" => "k"}]
arr2 = [{"one"=> {"8"=> "f", "11" => "r"}, "two" => {"7" => "o", "6" => "b", "14" => "b"}]
and I want to have one array like this:
arr3 = [{
"one"=> {"1"=> "a", "2" => "b", "8"=> "f", "11" => "r"},
"two" => {3" => 'n", "5" => "h", "7" => "k", 7" => 'o", "6" => "b", "14" => "b"}
]
so I want to merge hashes by keys and "add" their values. Can anyone help?
arr1 = [{"one"=>{"1"=>"a", "2"=>"b"}, "two"=>{"3"=>"n", "5"=>"h", "7"=>"k"}}]
arr2 = [{"one"=>{"8"=>"f", "11"=>"r"}, "two"=>{"7"=>"o", "6"=>"b", "14"=>"b"}}]
(arr1+arr2).each_with_object({}) { |g,h| h.update(g) { |_,o,n| o.merge(n) } }
# => {"one"=>{"1"=>"a", "2"=>"b", "8"=>"f", "11"=>"r"},
# "two"=>{"3"=>"n", "5"=>"h", "7"=>"o", "6"=>"b", "14"=>"b"}}
This uses the form of Hash#update (aka merge!) that uses a block ({ |_k,o,n| o.merge(n) }) to determine the value of the key _k when both hashes being merged have that key. (_ in _k tells the reader that that block variable is not used in the block calculation.) o and n are the values of that key in h and g respectively.
For each key k equal to "one" or "two", if the values (hashes) of arr1.first[k] and arr2.first[k] have a common key l, the merge operation will cause the value of l in arr1 will be overwritten by the value of l in arr2. If, for example, arr1.first["one"] #=> {"1"=>"a", "2"=>"b"} and arr2.first["one"] #=> {"8"=>"f", "2"=>"r"}, the merge will return {"1"=>"a", "2"=>"r", "8"=>"f"}
Even though arr1 and arr2 each contain a single element (a hash), the code above works fine when the arrays contain multiple hashes, and when there are more than two arrays. If the arrays always contain a single hash, the arrays serve no purpose and we might instead just reference the hashes:
h1 = {"one"=>{"1"=>"a", "2"=>"b"}, "two"=>{"3"=>"n", "5"=>"h", "7"=>"k"}}
h2 = {"one"=>{"8"=>"f", "11"=>"r"}, "two"=>{"7"=>"o", "6"=>"b", "14"=>"b"}}
and replace arr1+arr2 with [h1+h2].
Maybe not the most elegant but this works:
arr1 = [{"one"=>{"1"=>"a", "2"=>"b"}, "two"=>{"3"=>"n", "5"=>"h", "7"=>"k"}}]
arr2 = [{"one"=>{"8"=>"f", "11"=>"r"}, "two"=>{"7"=>"o", "6"=>"b", "14"=>"b"}}]
arr3 = []
arr1[0].each_key{|k| arr3<< {k => arr1[0][k].merge(arr2[0][k])}}
arr3
If you don't know how many hashes your original array will contain, simply replace arr1[0].each_key with arr1.each_index{|i| arr1[i].each_key and replace 0 with i in the merge.

Xcode Swift check if array contains object

I have this array :
var preferiti : [ModalHomeLine!] = []
I want to check if the array contains the same object.
if the object exists {
} else {
var addPrf = ModalHomeLine(titolo: nomeLinea, link: linkNumeroLinea, immagine : immagine, numero : titoloLinea)
preferiti.append(addPrf)
}
Swift has a generic contains function:
contains([1,2,3,4],0) -> false
contains([1,2,3,4],3) -> true
So it sounds like you want an array without duplicate objects. In cases like this, a set is what you want. Surprisingly, Swift doesn't have a set, so you can either create your own or use NSSet, which would look something like this:
let myset = NSMutableSet()
myset.addObject("a") // ["a"]
myset.addObject("b") // ["a", "b"]
myset.addObject("c") // ["a", "b", "c"]
myset.addObject("a") // ["a", "b", "c"] NOTE: this doesn't do anything because "a" is already in the set.
UPDATE:
Swift 1.2 added a set type! Now you can do something like
let mySet = Set<String>()
mySet.insert("a") // ["a"]
mySet.insert("b") // ["a", "b"]
mySet.insert("c") // ["a", "b", "c"]
mySet.insert("a") // ["a", "b", "c"] NOTE: this doesn't do anything because "a" is already in the set.

Resources