Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 248
YPCrumble Avatar asked Dec 18 '15 01:12

YPCrumble


2 Answers

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);
like image 51
Spencer Wieczorek Avatar answered Nov 15 '22 07:11

Spencer Wieczorek


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"]
like image 27
Paul S. Avatar answered Nov 15 '22 07:11

Paul S.