Skip to content

Commit

Permalink
Add selection.order.
Browse files Browse the repository at this point in the history
It's like selection.sort, except it assumes that the data is already sorted;
the elements are reordered to match the selection.
  • Loading branch information
mbostock committed Dec 9, 2011
1 parent fe671a7 commit 4a35d78
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 19 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ d3.core.js: \
src/core/selection-data.js \
src/core/selection-filter.js \
src/core/selection-map.js \
src/core/selection-order.js \
src/core/selection-sort.js \
src/core/selection-on.js \
src/core/selection-each.js \
Expand Down
19 changes: 11 additions & 8 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1759,19 +1759,22 @@ d3_selectionPrototype.map = function(map) {
this.__data__ = map.apply(this, arguments);
});
};
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = 0, m = this.length; j < m; j++) {
for (var group = this[j].sort(comparator), i = 1, n = group.length, prev = group[0]; i < n; i++) {
var node = group[i];
if (node) {
if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
prev = node;
d3_selectionPrototype.order = function() {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
if (node = group[i]) {
if (next) next.parentNode.insertBefore(node, next);
next = node;
}
}
}
return this;
};
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
return this.order();
};

function d3_selection_sortComparator(comparator) {
if (!arguments.length) comparator = d3.ascending;
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions examples/hello-world/hello-order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript">

var div = d3.select("body").selectAll("div")
.data(["a", "b", "f"])
.enter().append("div")
.text(String);

var div = d3.select("body").selectAll("div")
.data(["a", "b", "c", "d", "e", "f"], String);

div.enter().append("div")
.text(String);

div.order();

</script>
</body>
</html>
11 changes: 11 additions & 0 deletions src/core/selection-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
d3_selectionPrototype.order = function() {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
if (node = group[i]) {
if (next) next.parentNode.insertBefore(node, next);
next = node;
}
}
}
return this;
};
12 changes: 2 additions & 10 deletions src/core/selection-sort.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = 0, m = this.length; j < m; j++) {
for (var group = this[j].sort(comparator), i = 1, n = group.length, prev = group[0]; i < n; i++) {
var node = group[i];
if (node) {
if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
prev = node;
}
}
}
return this;
for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
return this.order();
};

function d3_selection_sortComparator(comparator) {
Expand Down
32 changes: 32 additions & 0 deletions test/core/selection-order-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require("../env");
require("../../d3");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("selection.order");

suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div")
.attr("id", String);
},
"orders elements by data": function(div) {
div = div.data([1, 10, 20, 2], String).order();
assert.domNull(div[0][0].previousSibling);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][2].previousSibling, div[0][1]);
assert.domEqual(div[0][3].previousSibling, div[0][2]);
assert.domNull(div[0][3].nextSibling);
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.order() === span);
}
}
});

suite.export(module);

0 comments on commit 4a35d78

Please sign in to comment.