Skip to content

Instantly share code, notes, and snippets.

@gunn
Created January 17, 2017 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gunn/49919c316ebea97d56c31955a607f11f to your computer and use it in GitHub Desktop.
Save gunn/49919c316ebea97d56c31955a607f11f to your computer and use it in GitHub Desktop.
D3 nest.sortKeys example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Nest Test</title>
</head>
<body>
<h3>Sorted?</h3>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var dates = Array(10).fill(0).map((_, i)=> new Date(i*10000000000 + +new Date()))
var itemsWithDates = dates.map(d=> ({date: d}))
const nest = d3.nest()
.key(d=> d.date)
.sortKeys(d3.descending)
.entries(itemsWithDates)
var sortedDates = nest.map(d=>d.values[0].date)
d3.select("body").append("ul")
.data(sortedDates)
.enter()
.append("li")
.text(d=> d.toLocaleDateString())
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment