简体   繁体   中英

How can I sort a javascript array while ignoring articles (A, an, the)?

I have the following sort function to sort a list of books:

var compare = function(a, b) {
  var aTitle = a.title.toLowerCase(),
      bTitle = b.title.toLowerCase();

  if (aTitle > bTitle) return 1;
  if (aTitle < bTitle) return -1; 
  return 0;
};
var sortedBooks = books.sort(compare);

How can I adjust this such that I ignore articles at the beginning of each title?

You can simply have a function say removeArticles() which checks if there is more than a single word in the sentence, if so return the second word for comparison. For specific words only you would need to add conditions for the words, for example (words[0] == 'a' || words[0] == 'the' || words[0] == 'an') would cover "A" , "An" , and "The" :

 books = ["A Whale", "The Moive", "A Good Time", "The Alphabet 2" , "The Alphabet 1", "Alphabet Soup", "Foo"]; var compare = function(a, b) { var aTitle = a.toLowerCase(), bTitle = b.toLowerCase(); aTitle = removeArticles(aTitle); bTitle = removeArticles(bTitle); if (aTitle > bTitle) return 1; if (aTitle < bTitle) return -1; return 0; }; function removeArticles(str) { words = str.split(" "); if(words.length <= 1) return str; if( words[0] == 'a' || words[0] == 'the' || words[0] == 'an' ) return words.splice(1).join(" "); return str; } var sortedBooks = books.sort(compare); // [ "The Alphabet 1", "The Alphabet 2", "Alphabet Soup", "Foo", "A Good Time", "The Moive", "A Whale" ] console.log(sortedBooks); 

You could use a RegExp to move them in your comparator. Also note that .sort has side-effects

function titleComparator(a, b) {
    var articles = ['a', 'an', 'the'],
        re = new RegExp('^(?:(' + articles.join('|') + ') )(.*)$'), // e.g. /^(?:(foo|bar) )(.*)$/
        replacor = function ($0, $1, $2) {
            return $2 + ', ' + $1;
        };
    a = a.toLowerCase().replace(re, replacor);
    b = b.toLowerCase().replace(re, replacor);
    return a === b ? 0 : a < b ? -1 : 1;
}

And putting this into practice

var books = [
    'A Wonderful Life', 'The Beginning', 'An Everlasting Tale', 'I go in the middle'
];

var sortedBooks = books.slice().sort(titleComparator); // note slice
// ["The Beginning", "An Everlasting Tale", "I go in the middle", "A Wonderful Life"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM