liveBook · Manning

Sorting algorithm

As explained in D3.js in Action, Third Edition

sort() is the JavaScript Array method that orders array elements according to an optional comparison function.

sort() orders array elements with an optional comparison function. In a bar chart, descending order makes the largest and smallest values immediately visible. section 3.2.3

Use sort() to prepare measured data for visualization:

  • Pass a comparison function that compares the count values of two technology objects. section 3.2.3
  • For descending order, place the object with the greater count before the other. section 3.2.3
  • Sort the data inside the Promise callback before passing it to the visualization-building function. section 3.2.3
data.sort((a, b) => b.count - a.count);

A focused implementation example sorts technology records by count in descending order. section 3.2.3

Sorting technology usage

data.sort((a, b) => b.count - a.count); places Excel, with 1,078, before Tableau, with 852, while Angular, with 20, appears last. section 3.2.3

How D3.js in Action, Third Edition covers it

How deep it goes
Demonstrates a comparison function and its placement in a data-processing workflow, without detailing broader sorting mechanics.
Examples
Uses a technologies dataset ordered by count, including Excel, Tableau, and Angular.
What you can do after
Apply via JavaScript sort() to order records by a numeric field before building a visualization.
How it connects
Connects JavaScript arrays with comparison functions, Promises, D3 data loading, and bar charts.

Also explained in

Compare how 18 books explain sorting algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.