This shows how to adjust Mike Bostock’s example of grouped bars such that you can separately sort the bars within groups or the groups themselves.
To separately sort the bars within groups see the comments A1 - A3. To sort the groups themselves see comment B1.