Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting objects based on property value in D3

I have a array of objects for use in D3 e.g

var cities = [
  { city: "London", country: "United Kingdom", index: 280 },
  { city: "Geneva", country: "Switzerland", index: 259 },
  { city: "New York City", country: "United States", index: 237 },
  { city: "Singapore", country: "Singapore", index: 228 },
  { city: "Paris", country: "France", index: 219 },
  { city: "San Francisco", country: "United States", index: 218 },
  { city: "Copenhagen", country: "Denmark", index: 217 },
  { city: "Sydney", country: "Australia", index: 215 },
  { city: "Hong Kong", country: "Hong Kong", index: 214 },
  { city: "Brisbane", country: "Australia", index: 208 }
}

I would like to order the objects in ascending order based on their cities.index property. So that I can display them as such in D3.js. Im sure there is a way of doing this in D3 but I am yet to figure it out when dealing with an array of objects.

Any help?

like image 341
Sam Mason Avatar asked Aug 06 '14 18:08

Sam Mason


People also ask

How do you sort an array of objects based on property?

Example 1: Sort Array by Property NameThe sort() method sorts its elements according to the values returned by a custom sort function ( compareName in this case). Here, The property names are changed to uppercase using the toUpperCase() method. If comparing two names results in 1, then their order is changed.

How do you sort a list of objects based on an attribute of the objects in JavaScript?

In JavaScript, we use the sort() function to sort an array of objects. The sort() function is used to sort the elements of an array alphabetically and not numerically. To get the items in reverse order, we may use the reverse() method.

How do you sort and array of objects?

To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.

How do you sort an object in an object?

No, it is not possible to sort an object. Objects are always unsorted. You might be better off having each row be an object entry in an array ; you could then sort the array by the customer name. You cannot rely on the iteration order of an object in JavaScript.


1 Answers

You can pass an anonymous function to the Javascript Array.prototype.sort to sort by index. D3 has a function d3.ascending (v 3.x) that makes it easy to sort ascending:

cities.sort(function(x, y){
   return d3.ascending(x.index, y.index);
})

And here's the output:

[
 {"city":"Brisbane","country":"Australia","index":208},
 {"city":"Hong Kong","country":"Hong Kong","index":214},
 {"city":"Sydney","country":"Australia","index":215},
 {"city":"Copenhagen","country":"Denmark","index":217},
 {"city":"San Francisco","country":"United States","index":218},
 {"city":"Paris","country":"France","index":219},
 {"city":"Singapore","country":"Singapore","index":228},
 {"city":"New York City","country":"United States","index":237},
 {"city":"Geneva","country":"Switzerland","index":259},
 {"city":"London","country":"United Kingdom","index":280}
]
like image 56
mdml Avatar answered Nov 13 '22 17:11

mdml