-2

Everyone, this is my Array Structure

let data = [
    {"name": "ragupathi", "siteID": 10},
    {"name": "abi","siteID": 13},
    {"name": "mahesh", "siteID": 12},
]

i want group data based on siteID so I am using groupBy siteID

{
  "10": [
    {
      "name": "ragupathi",
      "siteID": 10
    }

  ],
  "12": [
    {
      "name": "mahesh",
      "siteID": 12
    }
  ],
  "13": [
    {
      "name": "abi",
      "siteID": 13
    }
  ]
}

But I am Expecting output name in ASC order

{
  "13": [
    {
      "name": "abi",
      "siteID": 13
    }
  ],
  "10": [
    {
      "name": "mahesh",
      "siteID": 12
    }
  ],
  "12": [
   {
      "name": "ragupathi",
      "siteID": 10
    }
   ],
}

GroupBy SiteID Grouped Output based on properties of Object Name

I can't change the order the object based on the object property name give some example or reference code I am new to javascript

  • In the expected output the second key should `12` and third key should be `10` – Maheer Ali May 11 '19 at 10:49
  • @MaheerAli No, siteID is the object of key and another one condition is the final result should sortBy order based on the name property current output is 10 > 12 > 13 this order I am expecting order is based on the name property asc order 13>10>12 –  May 11 '19 at 10:54
  • 2
    Side-note: I’m a bit concerned, honestly, about the usage of object as an array-like structure. Why not just use array? As far as I know, it’s still not guaranteed that an object would preserve key order after a series of mutation (which groupBy does under the hood). Perhaps writing your own function that does grouping would make more sense in this situation. – rishat May 11 '19 at 10:55
  • @Fleischpflanzerl is there any possible to make this order without groupBy –  May 11 '19 at 10:57
  • What if there are multiple entries for a `siteID`? `{ "10": [{"name": "a", "siteID": 10},{"name": "z", "siteID": 10"}], "12":[{"name": "b", "siteID": 20}] }` – Andreas May 11 '19 at 10:57
  • The ordering of object properties is not something you can control. The spec dictates certain behavior, but it has to do with object lifecycle history, not anything meaningful. – Pointy May 11 '19 at 10:58
  • @Fleischpflanzerl I need an output on a name in asc order but i want key on siteID –  May 11 '19 at 10:58
  • 7
    I saw [the same question](https://stackoverflow.com/questions/56078819/change-the-order-of-groupby-result-data) yesterday. Things haven't changed since then - objects still don't allow you to change the order of their keys. – VLAZ May 11 '19 at 11:01
  • @Andreas yes the multiple Entity on-site ID `{ "10": [{"name": "a", "siteID": 10},{"name": "z", "siteID": 10"}], "12":[{"name": "b", "siteID": 10}] }` –  May 11 '19 at 11:01
  • Possible duplicate of [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Mark Schultheiss May 11 '19 at 11:19
  • If you don't mind the keys having a simple prefix, you could sort them.. eg.. `"_13": {` etc. – Keith May 11 '19 at 11:27

2 Answers2

-1

Why do you need an object with the sideID as key? You could just sort the array like this

let data = [
    {"name": "ragupathi", "siteID": 10},
    {"name": "abi","siteID": 13},
    {"name": "mahesh", "siteID": 12},
]

data.sort((a,b) => {    
  if(a.siteID > b.siteID) {
    return -1;
  }
  if(a.siteID < b.siteID) {
    return 1;
  }
  return 0;
})
Eyk Rehbein
  • 3,683
  • 3
  • 18
  • 39
  • `data` is grouped by `siteID`, that's why you get the object. Sorting the array doesn't to the same *and* OP expects the groups to then be sorted by `name`. To be honest, I'm not clear on why that is and what's with the grouping - if a group contains multiple items (like it could) which name do you sort on? – VLAZ May 11 '19 at 11:42
-1

I'm not 100% sure why an array wound't work for you.

Maybe you want 1 structure to be sorted & also access object using the key.

You might find that just using Array.find() would be pretty fast.

But another option, if you don't mind your keys having a prefix, eg. like _123, this will then force the key to be a string, as such using modern JS engines, the order is based on insertion order. So if you did a sort, and then reduce, you should get the object keys in the order you want, but of course with the simple prefix.

Of course if you later want to access the object, you just have to remember to put the prefix in.

const data = [
    {"name": "ragupathi", "siteID": 10},
    {"name": "abi","siteID": 13},
    {"name": "mahesh", "siteID": 12},
];


const ret =
  data.sort((a, b) => a.name.localeCompare(b.name)).
  reduce((a, v) => {
    a[`_${v.siteID}`] = v;
  return a;
}, {}); 

console.log(ret);

//test let's get id 13 by key
console.log(ret['_13']);

//prove order
console.log(Object.keys(ret));
Keith
  • 22,005
  • 2
  • 27
  • 44