repl.it
@paulhebert/

Sorting in Node with Secondary Characteristics

Nodejs

No description

fork
loading
Files
  • index.js
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const fetch = require('node-fetch');

// Fetch our data from the Github API
fetch("https://api.github.com/users/cloudfour/repos")
  .then(resp => resp.json())
  .then(repos => {
    // Remove all the extra fields besides name, language and id
    const cloudFourRepos = repos.map(({ name, language, id }) => ({
      name, 
      language: String(language),
      id,
    }))
    // Sort by programming language
    .sort((a, b) => {
      if (a.language === b.language) {
        return a.id < b.id ? -1 : 1;
      }
      return a.language < b.language ? -1 : 1;
    });

    console.table(cloudFourRepos);
  });
node v10.16.0