A Pen by Chris Barr

Thumbnail
This awesome code was written by chrismbarr, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright chrismbarr ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  Chris Barr</title>
  
  
  
  
  
</head>

<body>

  <h1>Open the console!</h1>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/chrismbarr/a-pen-by-chris-barr-jGBOVK */
console.clear();

const items = [
  {name: 'fff', mapped: false},
  {name: 'yyy', mapped: true},
  {name: 'uuu', mapped: true},
  {name: 'bbb', mapped: false},
  {name: 'ttt', mapped: false},
  {name: 'aaa', mapped: true},
  {name: 'qqq', mapped: false},
  {name: 'ppp', mapped: true},
  {name: 'vvv', mapped: true},
  {name: 'lll', mapped: false},
];

console.info("Unsorted");
console.table(items)

const sortedItems = items
  //First sort alphabetically
  .sort((a, b) => {
    let aName = a.name.toLowerCase();
    let bName = b.name.toLowerCase();
    if (aName < bName) { return -1; }
    if (aName > bName) { return 1;  }
    return 0;
  })
  //Then sort by the mapped property
  .sort((a, b) => {
    return (a.mapped === b.mapped) ? 0 : (a.mapped ? -1 : 1);
  });

console.info("Sorted")
console.table(sortedItems)

Comments