I have an array containing objects that I need to sort, and remove duplicates based on 3 specific values of each array item. Currently I’m using two loops(NOT nested): 1 to sort, another to remove items. Is there a way to remove duplicates while sorting or something similar? For 1000s of items the following code is quite slow and am wondering if there is a faster way:
var list = [
{a: "Somename", b: "b", c: 10},
{a: "Anothername", b: "a", c: 12},
// and so on
];
function sortList(list) {
// Sort the list based on the 3 specific values
list = list.sort(function(a,b) {
a.a = a.a.toLowerCase();
b.a = b.a.toLowerCase();
if (a.a < b.a) return -1;
if (a.a > b.a) return 1;
a.b = a.b.toLowerCase();
b.b = b.b.toLowerCase();
if (a.b < b.b) return -1;
if (a.b > b.b) return 1;
if (a.c < b.c) return -1;
if (a.c > b.c) return 1;
return 0;
});
// Loop through removing duplicates
for (var a,b,i=list.length-1; i > 0; i--) {
a = list[i];
a.a = a.a.toLowerCase();
a.b = a.b.toLowerCase();
b = list[i-1];
b.a = b.a.toLowerCase();
b.b = b.b.toLowerCase();
if (a.a===b.a && a.b===b.b && a.c===b.c) list.splice(i-1,1);
}
return list;
}
list = sortList(list);
Please no jQuery answers, or answers suggesting the use of another library. It seems a bit of an overkill to import a library to do something this simple.
After searching around, I came up with an answer of my own. It still uses the two loops but this is far faster than sorting then removing duplicates, and more consistent than tracking duplicates while sorting. Basically, I loop through the array, saving each item in an object using a concatenation of the property values(‘key’) I want to sort/remove duplicates by. If the key is already stored, I know it’s a duplicate and can move on to the next. Otherwise I add the item to a new array. Once duplicates are removed I sort the array and return it: