Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare arrays for equality, ignoring order of elements

I have a table with 4 array columns.. the results are like:

ids       signed_ids   new_ids   new_ids_signed {1,2,3} | {2,1,3}    | {4,5,6} | {6,5,4} 

Anyway to compare ids and signed_ids so that they come out equal, by ignoring the order of the elements?

like image 332
user766987 Avatar asked Oct 13 '12 05:10

user766987


People also ask

How do you compare two arrays if they are equal or not?

Check if two arrays are equal or not using Sorting Follow the steps below to solve the problem using this approach: Sort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.

How do you compare two lists irrespective orders in Java?

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

What is the correct way to compare array elements in the array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

Does order matter in array Java?

Java: Checking equality of arrays (order doesn't matter)


1 Answers

You can use contained by operator:

(array1 <@ array2 and array1 @> array2) 
like image 99
Selman Tunc Yilmaz Avatar answered Sep 30 '22 23:09

Selman Tunc Yilmaz