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

- 1 answer

Ad

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?

Ad

Answer

Ad

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);

Ad
source: stackoverflow.com
Ad