0

I have the following array of objects which every objects I want to sort based on below mentioned logic. I want to sort every object and use it for data visualisations.

 data=[
 {
 "Network":[78,89,22,89,24,20,92,72],
 "Usage":[45,3,87,32,87,56,83,65],
 "Congestion":[90,83,26,42,39,68,71,52]
  },
 {
  "Network":[73,69,34,65,24,95,75,42],
  "Usage":[42,76,94,34,65,24,84,24],
  "Congestion":[42,53,22,86,22,13,48,56]
 },
 {
  "Network":[73,77,93,28,90,13,42,9],
  "Usage":[16,63,11,85,24,58,52,22],
  "Congestion":[95,25,22,24,63,22,53,22]
 }]

Now I assign kpi=data[0] and run the following for loop,

for(q in kpi)
{
 r=0;
 for(p in kpi[q])
  {
  if(kpi[q][p]<70)r++;
  }
 kpi[q].red=r;
}

So I now want to sort the kpi on the basis of value of red. How to do it? Likewise I want to sort every obeject and use it for visualisation using D3 js. So any help using D3 js will be very worthy. Thanks in advance.

Varun Singh
  • 310
  • 2
  • 10
  • `So I now want to sort the kpi[0] on the basis of value of red.` How you are going to use value of red? – Moazzam Khan Sep 19 '13 at 14:43
  • 1
    *"So I now want to sort the `kpi[0]`..."* `kpi` doesn't have a `[0]` index. It's an object, not an Array. – user2736012 Sep 19 '13 at 14:43
  • 1
    @user2736012 Probably OP wants to sort `kpi` (`data[0]`), but it's also not possible, since objects can't be sorted. – Teemu Sep 19 '13 at 15:10
  • @Teemu: U guessed it correct. That is what I want to do. So, is there not any way to do, like creating a different array and mapping onto it. This is just my idea but I don't know how to do it. – Varun Singh Sep 19 '13 at 16:15
  • @varunsingh Yes, you can map for example `red`s and keys in `kpi` to an array, and then sort the array. The best way depends on how you're going to use the array and `kpi`. Notice also, that it's not a good idea to [iterate an array with `for..in`](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) loop, use regular `for` loop instead. – Teemu Sep 19 '13 at 18:08

0 Answers0