Sort Without Articles
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'];
const sortedBands = bands.sort(function (a, b) {
if(a > b) {
return 1;
} else {
return -1;
}
});
console.log(sortedBands);
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'];
function strip(bandName) {
return bandName.replace(/^(a |the |an)/i, '').trim();
}
const sortedBands = bands.sort(function (a, b) {
if(a > b) {
return 1;
} else {
return -1;
}
});
console.log(sortedBands);
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'];
function strip(bandName) {
return bandName.replace(/^(a |the |an)/i, '').trim();
}
const sortedBands = bands.sort((a, b) => strip(a) > strip(b) ? 1 : -1);
console.log(sortedBands);
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'];
function strip(bandName) {
return bandName.replace(/^(a |the |an)/i, '').trim();
}
const sortedBands = bands.sort((a, b) => strip(a) > strip(b) ? 1 : -1);
document.querySelector('#bands').innerHTML = sortedBands.map(band => `<li>${band}</li>`);
console.log(sortedBands);
문자열이 아닌 것을 innerHTML에 설정하려고하면, toStiring을 한것과 같이 표현됩니다.
그래서 배열과 배열 사이에 ,가 생긴것입니다.
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'];
function strip(bandName) {
return bandName.replace(/^(a |the |an)/i, '').trim();
}
const sortedBands = bands.sort((a, b) => strip(a) > strip(b) ? 1 : -1);
document.querySelector('#bands').innerHTML = sortedBands.map(band => `<li>${band}</li>`).join('');
console.log(sortedBands);
이를 해결하기 위해서 join을 통해 전체를 하나의 문장으로 만들어 줍니다.
결과를 보면 이제 제대로 나오는 것을 확인할 수 있습니다.
JavaScript30 강의를 보고 공부한 글입니다. (https://javascript30.com/)
'JavaScript30 Challenge' 카테고리의 다른 글
[JavaScript30] Day 22 (0) | 2020.08.17 |
---|---|
[JavaScript30] Day 18 (0) | 2020.07.30 |
[JavaScript30] Day 16 (0) | 2020.07.20 |
[JavaScript30] Day 15 (0) | 2020.06.25 |
[JavaScript30] Day 14 (0) | 2020.06.11 |