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 7767779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:45:33+00:00 2026-06-01T15:45:33+00:00

Background Using JavaScript, I need to sort a large JSON object based on a

  • 0

Background

Using JavaScript, I need to sort a large JSON object based on a given property of that object. I am assuming that a merge sort is the fastest approach. If this is not the fastest approach, please tell me what is. There are myriad examples online of a merge sort against an array, but very little with objects. Here is a sample object:

fruitForSale = {
     1: {"type":"orange","UnitPrice":0.20},
     2: {"type":"banana","UnitPrice":0.30},
     3: {"type":"pear","UnitPrice":0.10},
     4: {"type":"apple","UnitPrice":0.50},
     5: {"type":"peach","UnitPrice":0.70}
}

Question

Using a merge sort (or faster algorithm), how would I sort the fruitForSale object so that I end up with an object sorted by ‘type’:

   fruitForSale = {
                     4: {"type":"apple","UnitPrice":0.50},
                     2: {"type":"banana","UnitPrice":0.30},
                     1: {"type":"orange","UnitPrice":0.20},
                     5: {"type":"peach","UnitPrice":0.70},
                     3: {"type":"pear","UnitPrice":0.10}                  
                   }

NOTE: The original keys (1,2,3,4 & 5) would need to stay assigned to their respective object, so a key of 1 should always match with {"type":"orange","UnitPrice":0.20} and a key of 2 will always match with {"type":"banana","UnitPrice":0.30} and so on.

Thanks!

  • 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-01T15:45:34+00:00Added an answer on June 1, 2026 at 3:45 pm

    You can’t sort the keys on the object, but you can keep your own array of sorted keys.

    var fruitForSale = {
         1: {"type":"orange","UnitPrice":0.20},
         2: {"type":"banana","UnitPrice":0.30},
         3: {"type":"pear","UnitPrice":0.10},
         4: {"type":"apple","UnitPrice":0.50},
         5: {"type":"peach","UnitPrice":0.70}
    },
    
    sortedKeys = Object.keys(fruitForSale).sort(function (i,j) {
        return fruitForSale[i]["type"] > fruitForSale[j]["type"];
    });
    

    Example: http://jsfiddle.net/X2hFt/ (Output displayed on the console)

    Object.keys is not supported everywhere but you can polyfill if you need to easily. See:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys

    Oh, and if you are curious about the underlying implementation of sort see:

    Javascript Array.sort implementation?

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

Sidebar

Related Questions

I have given a table background image using css background-image property. The cells are
Background I've been using the C preprocessor to manage and compile semi-large javascript projects
background: i am using javascript templates, which i would like to output raw, without
Using Javascript, I would like change the background color of the numbers in an
Background: Using jQuery 1.7 client side PHP server side Using json responses with json_encode
I'm doing background subtraction using opencv. The problem is the foreground object is not
I need to be able to take a javascript string, compress it using any
In Javascript/JQuery I have a need to copy one elements CSS style object to
Background I'm working on a an educational JavaScript application/site (SPA) that will eventually have
I need to send data to a remote server using javascript. 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.