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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:11:07+00:00 2026-05-22T16:11:07+00:00

Having a problem trying to sort a JSON object. Basically, people can add products

  • 0

Having a problem trying to sort a JSON object. Basically, people can add products in any random order to our order form, but the order it shows in the summary needs to be how we want them to be positioned (not the order they select them), so thats why I need to sort by ‘id’ (or we’ll sort by a ‘pos’ field later)

Essentially, I need to sort by the id ascending. 1,2,103 instead of 2,103,1

I seem to be having issues because the index into the individual objects are numbers (or just the fact that they’re there…).

I need to do something along the lines of array.sort(function(a,b){ return a.id-b.id }); but I’m presuming that doesn’t work because 1, its not an array (its an object), and 2, it has those pesky indexes (that i need for another part of my code)…

Any ideas????

var products = {
    "2": {
        "id": "2",
        "price": "119",
        "quantity": "1",
        "thumb": "img\/store\/comp-08n.png"
    },
    "103": {
        "id": "103",
        "price": "109",
        "quantity": "1",
        "thumb": "img\/store\/basketballhoop.png"
    },
    "1": {
        "id": "1",
        "price": "309",
        "quantity": "1",
        "thumb": "img\/store\/comp-08.png"
    }
};
  • 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-22T16:11:08+00:00Added an answer on May 22, 2026 at 4:11 pm

    How many items do you need in your orders? You can safely sort 10’000 items in a Javascript array without much of speed issues. Why don’t you work with a real array instead?

    You could even inject custom properties to it, roughly something like

    var products = [...];
    
    products.findById = function(id) {
       for (var i=0, len=this.length; i<len; i++) {
          if (id == this[i].id) return this[i];
       }
       return null;
    };
    
    alert( products.findById(103).price );   // -> 119
    

    and add predefined sorters like

    products.sortById = function() {
        this.sort(function(a,b) {
            return a.id - b.id;
        });
    };
    
    products.sortById();   // sort array by product id
    

    ** EDIT **

    On your PHP side, you might have something like :

    $products = array(
        2 => array( 'id' => 2, ... ),
        103 => array( 'id' => 103, ... ),
        1 => array( 'id' => 1, ... ),
    );
    
    // get a JSON array
    $jsonArray = json_encode(array_values($products));
    

    will return what you need.

    ** NOTE **

    You should not explicitly set indexes when adding new items in your array. Use the array’s push function, like

    products.push({id:123, price:200.49, quantity:1, thumb:'/path/to/file'});
    

    Removing an item is a bit tricky, however, something like :

    products.removeById = function(id) {
       for (var i=0, len=this.length; i<len; i++) {
          if (id == this[i].id) return this.splice(i, 1)[0];
       }
       return null;
    };
    
    products.removeById(123);    // -> returns the removed element! or null if nothing was removed
    

    See demo here (use Chrome developper tools for console output).

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

Sidebar

Related Questions

I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm having a problem parsing a JSON file in AS3. Im trying to parse
I am having problem trying to sort an array - I want all the
I am having a problem trying to sort with an XSL file using the
I'm having a little problem with trying to sort the contents of a table
I'm having a problem with the following VS 2010 code. Am trying to sort
Having a problem trying to create a function, as part of a BizTalk helper
I am having a problem trying to use the prependTo() function in jQuery... for
I'm having a problem trying to format the output on the jQuery UI datepicker.
I'm having a problem when trying to pass an array back to a COM

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.