I've got 2 JSONArray full of integers.
I want to compare them for equal content without regard for order.
So:
[1, 2] == [1, 2] TRUE [1, 2] == [2, 1] TRUE
JSONArray has
public boolean equals(Object o)
but it returns FALSE for [1, 2] == [2, 1]
So, I rolled my own:
public boolean isEqual(JSONArray inputJsonArray,
JSONArray outputJsonArray) throws JSONException{
boolean equal=true, done;
int idx = 0;
if (inputJsonArray.length() == outputJsonArray.length()){
//make sure all elements in input array are in output array
done=false;
while (!done){
if(idx >= inputJsonArray.length()){
done=true;
}
else if (isIntInJsonArray(outputJsonArray,
inputJsonArray.getInt(idx)) == false){
equal = false;
done=true;
}
else{
idx ++;
}
}
if (equal){
//make sure all elements in output array are in input array
done=false;
while (!done){
if (idx >= outputJsonArray.length()){
done=true;
}
else if (isIntInJsonArray(inputJsonArray,
outputJsonArray.getInt(idx)) == false){
equal = false;
done=true;
}
else{
idx++;
}
}
}
}
else{
equal = false;
}
return equal;
}
Basically, I check if both JSONArrays are the same length. If they are then I make sure every element in the outputJsonArray is in the inputJsonArray and vice versa. The workhorse method that does this is:
private boolean isIntInJsonArray(JSONArray inputJsonArray, int mInt) throws JSONException{
boolean found=false, done=false;
int idx = 0;
while (!done){
if(idx >= inputJsonArray.length()){
done=true;
}
else if (inputJsonArray.getInt(idx) == mInt){
found = true;
done=true;
}
else{
idx ++;
}
}
return(found);
}
This strikes me like an awful lot of code. Does anyone know if there is a simpler way to do this?