Robert Andersson
Robert Andersson

Reputation: 1521

How to re-sort a barchart into it's initial state after sorting in d3js

I've made a plunker were you can sort the data from largest to lowest value, which is great, but when you uncheck the box it sorts alphabetically like so

function(a, b) { return d3.ascending(a.name, b.name); }

I have no idea what to replace this with so that it just return to it's initial state before you checked the box, any help would be appreciated!

Upvotes: 1

Views: 32

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102174

First, create an array with the correct order of your names:

var sortIndex = data.map(function(d){ return d.name});

And then use it for sorting:

function(a, b) { 
    return sortIndex.indexOf(a.name) - sortIndex.indexOf(b.name); 
}

Here is your updated plunker: https://plnkr.co/edit/SmpDOk7KYSvrM2ioUoxE?p=preview

Upvotes: 1

Related Questions