I am comparing zip codes.
I have three constants of zip codes :
ZIP_MORRIS ZIP_UNION ZIP_ESSEX
I want to see if a user has in an Object's array, all the zips included in one of those.
I tried this:
ZIP_UNION.sort{|x,y| y <=> x} <=> Email.find(3).distributions.map(&:zip_code).uniq.compact.sort{|x,y| y <=> x}
But unfortunately, this just maps all the zip codes, so if I were to choose one extra zip in a different county, then it would not properly compare them.
I think the best solution would be to compare the values of the User Generated Zips, and see if all of the zips in one ZIP_COUNTY are present inside the array.
Some kind of iterator that would run through all the zips and ensure that the user's zip do or do not include every single zip in a zip group.
Any ideas?
To check if two arrays have the same elements:Use the every() to check if the arrays contain the same element at the same index. The every method only returns true if the condition is met for all array elements.
Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).
In short, to compare two dimensional arrays we have implemented a method as described below: The example's method is boolean equal(final int[][] arr1, final int[][] arr2) . The method takes as parameters two int arrays, and returns a boolean , that is true if the arrays are equal and false otherwise.
You can do array differences, if the result is the empty array, the 2 arrays contained the same elements:
>> [1,2,3]-[3,1,2] #=> []
If you still have elements left, then not all elements of the first array were present in the second one:
>> [1,2,5]-[3,1,2] #=> [5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With