Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dedfd38

Browse files
committed
get hint: use Array.sort() for wesbos#17. got array sorted properly. TODO: display in HTML.
1 parent 2a2e996 commit dedfd38

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

17--Sort_Without_Articles/index-jds.html

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,38 @@
4545
<script>
4646
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'];
4747
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;
5671
}
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;
6575
}
66-
// console.groupEnd();
76+
77+
return 0;
78+
6779
});
68-
console.log(bandsMapped);
69-
const bandsSorted = bandsMapped.sort();
7080
console.log(bandsSorted);
7181

7282
</script>

0 commit comments

Comments
 (0)