A few things to point out:
a
should not be used as a variable name; while it may work, programming convention is to use that variable name for sorting (in some languages, using it in open code this could cause problems)
a
and arr
are pointing to the same array in memory; since you are not copying all the elements (and subelements) of arr
into a
, they will both be referencing the same value, so a
is useless and the sort will be applied to arr
as @Saar has pointed out, since all your elements determine the uniqueness of your array, you can cast the array as a string and compare that value to the string of the next array
See:
console.clear();
var arr = [
["OPEL", "12365", "TY"],
["FORD", "52874", "QW"],
["OPEL", "96542", "TY"],
["FIAT", "45621", "TY"],
["FIAT", "74125", "QW"],
["FORD", "52874", "QW"]
];
function sortAlphabetically(a,b){
var a_lower = a.toString().toLowerCase(),
b_lower = b.toString().toLowerCase();
// sort ascending (a comes first)
if (a_lower < b_lower)
return -1;
// b comes first (move a behind b)
if (a_lower > b_lower)
return 1;
// default: a and b are the same (no sort)
return 0;
}
function FindDuplicates(){
arr.sort(sortAlphabetically);
for (var i = 0; i < arr.length-1; i++){
if (arr[i+1]+'' == arr[i]+''){
console.log('Duplicate at: ',
[
'[', i, ']',
' and ',
'[', i+1, ']',
'\n\tvalue: "',arr[i],'"'
].join(''));
}
}
}
FindDuplicates();