|
45 | 45 | <script>
|
46 | 46 | const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];
|
47 | 47 | console.log(bands);
|
48 |
| -const articles = ['a ','an ','the ']; |
49 |
| -// const bandsSorted = bands.sort(); |
50 |
| -const bandsMapped = bands.map(band => { |
51 |
| - band = band.toLowerCase(); |
52 |
| - // console.group('START bandsSorted: ', band); |
53 |
| - for (var i = 0; i < articles.length; i++) { |
54 |
| - console.log('\"'+articles[i]+'\" @ '+band.indexOf(articles[i])); |
55 |
| - // console.log(band.indexOf(articles[i])); |
| 48 | + |
| 49 | +const articlesArr = ['a ','an ','the ']; |
| 50 | +const articlesRegex = /^a\s|^an\s|^the\s/gi; |
| 51 | + |
| 52 | +const bandsSorted = bands.sort(function(a,b){ |
| 53 | + console.group('START bandsSorted'); |
| 54 | + console.log(a+' || '+b); |
| 55 | + |
| 56 | + // console.log(a.match(articlesRegex)); |
| 57 | + const aStart = a.match(articlesRegex)!==null ? a.match(articlesRegex)[0].length : 0; |
| 58 | + // console.log(aStart); |
| 59 | + a = a.substr(aStart); |
| 60 | + |
| 61 | + // console.log(b.match(articlesRegex)); |
| 62 | + const bStart = b.match(articlesRegex)!==null ? b.match(articlesRegex)[0].length : 0; |
| 63 | + // console.log(bStart); |
| 64 | + b = b.substr(bStart); |
| 65 | + |
| 66 | + console.log(a+' || '+b); |
| 67 | + console.groupEnd(); |
| 68 | + |
| 69 | + if (a > b) { |
| 70 | + return 1; |
56 | 71 | }
|
57 |
| - if (band.indexOf('a ') === 0) { |
58 |
| - return band.substring(2); |
59 |
| - } else if (band.indexOf('an ') === 0) { |
60 |
| - return band.substring(3); |
61 |
| - } else if (band.indexOf('the ') === 0) { |
62 |
| - return band.substring(4); |
63 |
| - } else { |
64 |
| - return band; |
| 72 | + |
| 73 | + if (a < b) { |
| 74 | + return -1; |
65 | 75 | }
|
66 |
| - // console.groupEnd(); |
| 76 | + |
| 77 | + return 0; |
| 78 | + |
67 | 79 | });
|
68 |
| -console.log(bandsMapped); |
69 |
| -const bandsSorted = bandsMapped.sort(); |
70 | 80 | console.log(bandsSorted);
|
71 | 81 |
|
72 | 82 | </script>
|
|
0 commit comments