Avatar of Bharat Guru
Bharat Guru
 asked on

How to order json in javascript

How to order below json by Location , Name ="Total" comes before detail

{
   "records":[
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Qty":16,
         "Name":"Total"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Qty":11,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Qty":5,
         "Name":"Total"
      },
        {
         "Location":"US",
         "RecordType":"Grocery",
         "Qty":17,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Productid":123,
         "Qty":5,
         "Name":"Apple"
      },
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Productid":124,
         "Qty":11,
         "Name":"Bananna"
      },

      {
         "Location":"US",
         "RecordType":"Grocery",
         "Productid":223,
         "Qty":6,
         "Name":"Rice"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Productid":224,
         "Qty":11,
         "Name":"Beans"
      }
   ]
}

Open in new window




How to rearrange json so Location + Total comes before detail

{
   "records":[
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Qty":16,
         "Name":"Total"
      },
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Productid":124,
         "Qty":11,
         "Name":"Bananna"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Qty":11,
         "Name":"Total"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Productid":224,
         "Qty":11,
         "Name":"Beans"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Qty":5,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Productid":123,
         "Qty":5,
         "Name":"Apple"
      },
        {
         "Location":"US",
         "RecordType":"Grocery",
         "Qty":17,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Grocery",
         "Productid":223,
         "Qty":6,
         "Name":"Rice"
      }
   ]
}

Open in new window

JavaScriptJSON

Avatar of undefined
Last Comment
Bharat Guru

8/22/2022 - Mon
ste5an

By changing the order in your object.

But you're aware that order is absolutly meaningless in this context?
Bharat Guru

ASKER
But some times in json total comes before detail and I want to put total always first
Bharat Guru

ASKER
Some times detail comes before total and
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bernard Savonet

Ste5an pointed that the order is probably meaning less, especially if the json is read by another program somewhere///
Bharat Guru

ASKER
In json some time the order is as below and I want to generate the table using javascript where total comes before detail.
{
    "records":[
  {
          "Location":"UK",
          "RecordType":"Fruits",
          "Productid":124,
          "Qty":11,
          "Name":"Bananna"
       },
     {
          "Location":"UK",
          "RecordType":"Fruits",
          "Productid":124,
          "Qty":11,
          "Name":"Bananna"
       },
            {
          "Location":"UK",
          "RecordType":"Fruits",
          "Productid":230
          "Qty":25
          "Name":"Peach
       },
            {
          "Location":"UK",
          "RecordType":"Grocery",
          "Productid":224,
          "Qty":11,
          "Name":"Basmati Rice"
       },
       {
          "Location":"UK",
          "RecordType":"Fruits",
          "Qty":16,
          "Name":"Total"
       },
       {
          "Location":"UK",
          "RecordType":"Grocery",
          "Qty":11,
          "Name":"Total"
       }
        ]
 }


Table should as below
Location, RecordType, Qty, Name
UK,  Fruits, Total , qty
UK,  Fruits, products , qty
UK,  Fruits, products , qty
UK,  Fruits, products , qty
...
UK,  Grocery, Total , qty
UK,  Grocery, products , qty
UK,  Grocery, products , qty
UK,  Grocery, products , qty
..
UK,  GroceryType2, Total , qty
UK,  GroceryType2, products , qty
UK,  GroceryType2, products , qty
UK,  GroceryType2, products , qty
..
Bernard Savonet

Your table is going to be displayed by some program.
As far as I understand, you currently read  each object and display it asap.

You might consider silghtly alterating you program so that it keeps all objetcs / lines until they are all read, and then display them in the order you want.

You did not mention which program is generating the output. Is it a javascript program to whch you have access?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bharat Guru

ASKER
yes my json string is in javascript
ste5an

In json some time the order is as below and I want to generate the table using javascript where total comes before detail.
Then just sort the data before creating/displaying your table.
ASKER CERTIFIED SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bharat Guru

ASKER
Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy