diff --git a/docz/docs/02-getting-started/02-example.mdx b/docz/docs/02-getting-started/02-example.mdx index d8c73d5..8cdc610 100644 --- a/docz/docs/02-getting-started/02-example.mdx +++ b/docz/docs/02-getting-started/02-example.mdx @@ -79,6 +79,31 @@ complete filter would be: const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez")); ``` +### Sorting by First Term + +The dataset is sorted in chronological order by the first presidential or vice +presidential term. The Vice President and President in a given term are sorted +alphabetically. Joe Biden and Barack Obama were Vice President and President +respectively in 2009. Since "Biden" is lexicographically before "Obama", Biden's +data point appears first. The goal is to sort the presidents in order of their +presidential term. + +The first step is adding the first presidential term start date to the dataset. +`Array#find` will find the first value in an array that matches a criterion. +The following code looks at each president and creates a `"start"` property that +represents the start of the first presidential term. + +```js +prez.forEach(prez => prez.start = prez.terms.find(term => term.type === "prez").start); +``` + +`Array#sort` will sort the array. Since the `start` properties are strings, the +recommended approach is to use `String#localeCompare` to compare strings: + +```js +prez.sort((l,r) => l.start.localeCompare(r.start)); +``` + ### Reshaping the Array For this example, the name will be the first name combined with the last name @@ -200,6 +225,10 @@ function Presidents() { return (