Solution 1 :

You can do that with sort:

input = { "data_report": [{ "data": [1, 2, 0, 3], "label": "Test2", "backgroundColor": "blue" }, { "data": [3, 4, 2, 5], "label": "test3", "backgroundColor": "#a3eaae" }, { "data": [2, 3, 1, 4], "label": "test4", "backgroundColor": "#37bd11" }, { "data": [1, 2, 0, 3], "label": "test7", "backgroundColor": "#43bee3" }, { "data": [1, 2, 0, 3], "label": "test5", "backgroundColor": "#a3eaae" }, { "data": [0, 1, 0, 2], "label": "test6", "backgroundColor": "#1195bd" }, { "data": [0, 1, 0, 2], "label": "test1", "backgroundColor": "#aeb5b7" }, { "data": [1, 2, 0, 3], "label": "test7", "backgroundColor": "pink" } ], "weeks": ["Week 1 ", "Week 2 ", "Week 3 ", "Week 4 "]};

input.data_report = input.data_report.sort((a,b)=>a.label.match(/d+/)[0]-b.label.match(/d+/)[0]);

console.log(input);

Solution 2 :

Yes you can use .sort method on the array

var sorted = input.data_report.sort((item)=>{return item.label;});

Something along those lines.

Solution 3 :

You need the JSON to be sorted before appending to HTML. I suggest something like sort function.

_.map(_.sortBy(json, 'key'), 'value'); (this one using lodash)

Problem :

I have the json response which I want to make the order based on the label .Is it possible to arrange the json response data based on the stander fixed order.want to arrang in the order defined any required json let order={"data":["test1","test2","test3","test4","test5","test6","test7","test8"]}(text may vary).Is it possible to arrange in order

let order={"data":["test1","test2","test3","test4","test5","test6","test7","test8"]}
const input = {
  "data_report": [{
      "data": [1, 2, 0, 3],
      "label": "user2",
      "backgroundColor": "blue"
    },
    {
      "data": [3, 4, 2, 5],
      "label": "test3",
      "backgroundColor": "#a3eaae"
    },
    {
      "data": [2, 3, 1, 4],
      "label": "test4",
      "backgroundColor": "#37bd11"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "test7",
      "backgroundColor": "#43bee3"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "user5",
      "backgroundColor": "#a3eaae"
    },
    {
      "data": [0, 1, 0, 2],
      "label": "test6",
      "backgroundColor": "#1195bd"
    },
    {
      "data": [0, 1, 0, 2],
      "label": "test1",
      "backgroundColor": "#aeb5b7"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "test7",
      "backgroundColor": "pink"
    }
  ],
  "weeks": ["Week 1 ", "Week 2 ", "Week 3 ", "Week 4 "]
}
var sorted = input.data_report.sort((item)=>{return order.data;});
console.log( sorted);
<div class="test"></div>

Comments

Comment posted by Heretic Monkey

There is no JSON in this question. JSON is a text format. What you have in

Comment posted by ROOT

do you mean that you want the output to be,

Comment posted by user3386779

please my edited post and snippet

Comment posted by ROOT

@user3386779, can you edit the question, and add the output you want, its not clear what the output should be?

Comment posted by How do I sort an array of objects based on the ordering of another array?

Does this answer your question?

Comment posted by user3386779

please see my updated expected string.I don’t want to arrange in alphabetical order. .I need it in defined set of json as I mentioned

Comment posted by gorak

@user3386779, you can definitely do that.I’ll edit my answer.

Comment posted by David Torroija

@user3386779 this is the correct answer is not ordering alphabetically its ordering first by the last decimal digit in the string so it’s solving your problem, vote this answer

By

Leave a Reply

Your email address will not be published. Required fields are marked *