node_modules
.alm/
lib
# About

Code for a lesson on egghead.io.

[View all my lessons here 🌹](https://egghead.io/instructors/basarat-ali-syed/)
<!DOCTYPE html>
<html>
  <body>
    <!-- // make console.log will write to the page for better in-browser experience -->
    <script>
      (function () {
    var body = document.querySelector('body');
    body.style['fontFamily'] = 'monospace';
    body.style['fontSize'] = '2em';
    console.log = function (x) { body.innerText += x + '\n'; };
    }());
  </script>
  <script src="src/index.js"></script>
  </body>
</html>
{
  "name": "demo",
  "version": "0.0.0",
  "description": "demo",
  "repository": "https://twitter.com/basarat",
  "main": "lib/index.js",
  "typings": "lib",
  "scripts": {
    "build": "tsc -p .",
    "resume": "git add . && git commit -m 'push' && git pull --rebase && git push"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@types/node": "^7.0.10",
    "typescript": "^2.1.4"
  }
}
# Case insensitive sorting for string arrays
> We look at the default `Array.prototype.sort` behavior and discuss how you can do case insensitive string sorting.

Here we have an array of a few strings.

```js
const foo = [
  'Alpha',
  'beta',
  'Gamma',
  'delta'
];
foo.sort();
foo.forEach(x=>console.log(x));
```

* If we go ahead and sort them you can see that `Gamma` appears before beta.
* This is because the default string sorting in JavaScript is case sensitive and capital case strings come before lower case strings in their unicode code points so capital G will appear before lowercase b and d.

```js
// A a
```

* To do case insensitive sorting you can provide a custom comparer function to Array prototype sort.
* You take two strings
* then you use the string prototype localeCompare function for the first string compare it to the  second.

```js
foo.sort((a, b) => a.localeCompare(b));
```

* Now there are a few ways of adding case insensitivity to this but the simplest one with the greatest browser support is to simply convert both strings to lower case before doing the comparison.

```js
const foo = [
  'Alpha',
  'beta',
  'Gamma',
  'delta'
];
foo.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
foo.forEach(x => console.log(x));
```
const foo = [
  'Alpha',
  'beta',
  'Gamma',
  'delta'
];
foo.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
foo.forEach(x => console.log(x));
{
  "compilerOptions": {
    "target": "es5",
    "outDir": "lib",
    "lib": [
      "es6",
      "dom"
    ]
  },
  "include": [
    "src"
  ]
}