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

  • Home
  • SEARCH
  • 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 908169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:40:26+00:00 2026-05-15T16:40:26+00:00

I have a JSON object that I want to sort by one key first,

  • 0

I have a JSON object that I want to sort by one key first, then by a second key similar to ordering by two columns in SQL. Here is a sample of the JSON I would have:

{
   "GROUPID":3169675,
   "LASTNAME":"Chantry"
}

I would like to order all the results by the GROUPID and then by LASTNAME. I’ve used the JSON sort function to sort by one key but not multiple.

Any help would be great.

  • 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-05-15T16:40:26+00:00Added an answer on May 15, 2026 at 4:40 pm

    Assuming you have an array of objects:

    var data = [
        { "GROUPID":3169675, "LASTNAME":"Chantry" },
        { "GROUPID":3169612, "LASTNAME":"Doe" },
        ...
    ];
    

    You can use a custom comparator to do the sorting. To sort first by GROUPID, and then by LASTNAME, the logic to compare two objects would be:

    if GROUPID of first is smaller than second
        return -1;
    else if GROUPID of first is larger than second
        return 1;
    else if LASTNAME of first is smaller than second
        return -1;
    else if LASTNAME of first is larger than second
        return 1;
    else
        return 0;
    

    To sort the object array, use the above algorithm and call the sort method on the array. After sorting is done, data should have the elements in required sorted order.

    data.sort(function(a, b) {
        // compare a and b here using the above algorithm
    });
    

    Here’s another very similar question I answered recently. It’s regarding sorting on multiple columns using jQuery, but you can strip out the jQuery part easily. It presents some customizable approaches which can extend to multiple columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a JSON object that is a 2-dimentional array and I need
I have a request that returns a JSON object with a single property which
I have two JSON objects with the same structure and I want to concat
I have one JSON that is coming in a string format. I need to
I have all this JSON text that I want to deserialize (or something) into
ok i have an ajax call that recives a json object. the idea of
I have an application which is sending me JSON object. Now I want to
I have created a json object from ruby with cobravsmongoose, however the attributes have
say I have the following json object; {'fname':'john', 'lname':'Locke'} and the following text boxes
I have a JSON as follows { columns : [RULE_ID,COUNTRY_CODE], RULE_ID : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], COUNTRY_CODE

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.