Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:15:27+00:00 2026-06-09T23:15:27+00:00

I have an array containing objects that I need to sort, and remove duplicates

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:15:29+00:00Added an answer on June 9, 2026 at 11:15 pm

    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:

    function sortList(list){
        // Since the 3 properties I'm sorting by can be converted to string I can loop
        // through the specified array, creating 'keys' by concating those property values
        // and adding those 'keys' to a temporary object. If the 'key' is present in the
        // temporary object, I know it's a duplicate in the array and can ignore it.
        // Otherwise I know it's not a duplicate and add it to a new array
        for (var a=0,b=list.length,item,key="",dupObj={},nList=[]; a<b; a++) {
            item=list[a];
            key=item.a.toLowerCase()+"_"+item.b.toLowerCase()+"_"+item.c;
            if(!dupObj[key]) {
                dupObj[key]=true;
                nList.push(item);
            }
        }
    
        // Now that we have an array without duplicates we can sort the array:
        nList.sort(function(a,b) {
            if ((a.a = a.a.toLowerCase()) < (b.a = b.a.toLowerCase()) return -1;
            if (a.a > b.a) return 1;
    
            if ((a.b = a.b.toLowerCase()) < (b.b = b.b.toLowerCase())) 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;
        });
    
        // return the new list
        return nList;
    }
    var list = [
        {a: "Somename", b: "b", c: 10},
        {a: "Anothername", b: "a", c: 12},
        // and so on
    ];
    list = sortList(list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Objects, each containing a Location and a Links array
I have several string each containing a JSON representation of an array of objects.
I have an array containing a list of files. I want to sort it
I have an array containing specific times and I want when the current time
Ruby 1.8.6 I have an array containing numerical values. I want to reduce it
I have an int array containing gray scale values from 0-254, i also have
Currently I need to set up a database that uses the following structure. Item
I have an object array containing array of a different type that is not
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a array containing japanese caracters as well as normal. How do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.