Json 使用d3js进行排序

Json 使用d3js进行排序,json,d3.js,Json,D3.js,我想按升序对json数据进行排序,升序包含标题、价格。 我想按价格分类 这是我在d3js中使用的json数据 var data = [ {"title":"ACER Aspire DQ.SP3EK.001 ZS 605 Refurbished 19.5\u201d All-in-One PC", "price":"<549.99"}, {"title":"LENOVO C540 23\u201d Touchscreen All-in-One PC","price":"699.99"}, {

我想按升序对json数据进行排序,升序包含标题、价格。 我想按价格分类

这是我在d3js中使用的json数据

var data = [
{"title":"ACER Aspire DQ.SP3EK.001 ZS 605 Refurbished 19.5\u201d All-in-One PC",
"price":"<549.99"},
{"title":"LENOVO C540 23\u201d Touchscreen All-in-One PC","price":"699.99"},
{"title":"ACER Aspire V5-122P 11.6\u201d Touchscreen Laptop/u2013silver","price" :"499.99"},
{"title":"HP Pavilion 15-n097ea 15.6 Laptop \u2013 Goji Berry","price":"429.99"},
{"title":"SONY Vaio Fit 15 E SVF1521A1EW.CEK 15\u201d Laptop \u2013 White","price":"399.99"}

] 

通过将比较器函数传递给
.sort()
,可以对数组进行排序:


完整示例。

您需要
.sort(d3.ascending(函数(d){return d.price;}))
,但这不能正确处理字符串。我建议您在JSON中只使用实际的数字,然后在其他地方进行格式化。是的,我已经尝试过了,但它对我不起作用,我在JSON中有近40条记录。正如我所说的,尝试将价格仅作为数字。唱片的数量无关紧要,我也试过。但是它对我不起作用。你能发布你尝试过的代码和数据吗?如果我的json是[{“price”:“£;1499.99”},{“price”:“£;1199.99”},{“price”:“£;99.99”}],并使用它对它进行排序,那么它在逗号值之后的排序为什么。。。???输出显示为:1999.99英镑1499.99英镑99.99英镑,但原始输出应显示:99.99英镑1499.99英镑1999.99这是字符串排序顺序。如果需要数字顺序,则必须使用数字(数据类型)。
 var container = gallery.selectAll('.searchcontainer')
.data(data, function(d) { return d.title; });

container.enter().append('div')
.attr('class', 'searchcontainer')

container.exit().remove();

container.selectAll('ttl')
.data(function(d) { return [d]; })
.enter().append('div')
.attr('class', 'ttl')
.style('float','right')
.style('margin-top','-20px')
.style('margin-right','20px')
.style('width','80%')
.html(function(d) { return d.title; });

container.selectAll('.pricess')
.data(function(d) { return [d]; })
.enter().append('div')
.attr('class', 'pricess')
.style('float','right')
.style('width','10%')
.style('margin-bottom','10px')
.sort(d3.ascending())
.html(function(d) { return d.price; });
var container = gallery.selectAll('.searchcontainer')
  .data(data.sort(function(a,b) { return +a.price - +b.price; }),
    function(d) { return d.title; });