最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

var pare = 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(pare);

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

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

var pare = 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(pare);

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

Share Improve this question asked Dec 18, 2015 at 1:06 YPCrumbleYPCrumble 28.8k25 gold badges111 silver badges175 bronze badges 4
  • What do you mean by "articles at the beginning of each title"? Do you mean that each titles has more than a single work such as "A Moive" and you want to make it into "Moive" for parison? – Spencer Wieczorek Commented Dec 18, 2015 at 1:08
  • 6 The OP means a substring. "A New Hope" should be alphabetized with the N's, and "The Force Awakens" should be alphabetized with the F's. "The Phantom Menace" should just be deleted, but that's a different problem than the OP is looking to solve. – jimm101 Commented Dec 18, 2015 at 1:11
  • Your pare just needs to test to see if the strings start with an article, and remove it if it exists. – Nathan Merrill Commented Dec 18, 2015 at 1:11
  • To be clear you are sorting only 2 inputs at a time? – Daniel Commented Dec 18, 2015 at 1:19
Add a ment  | 

2 Answers 2

Reset to default 7

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 parison. 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 pare = 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(pare);

// [ "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 parator. 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"]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论