Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Last active February 8, 2016 18:40
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 biovisualize/6e089db6771df328d223 to your computer and use it in GitHub Desktop.
Save biovisualize/6e089db6771df328d223 to your computer and use it in GitHub Desktop.
Selection sort
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
div{
width: 50px;
height: 50px;
}
</style>
</head>
<body>
<script>
var divs = d3.select('body').selectAll('div')
.data([
{foo: 1, bar: 3, color: 'red'},
{foo: 2, bar: 2, color: 'blue'},
{foo: 3, bar: 1, color: 'green'}
])
.enter().append('div')
.style({
'background-color': function(d){ return d.color; }
});
divs.sort(function(a, b){ var sortKey = 'foo'; return a[sortKey] - b[sortKey]; });
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment