2

I am looking to sort an array containing movie title strings such as:

['Battleship','101 Dalmatians','Alice, Sweet Alice','40 Days of Night', '303 Fear Faith Revenge'...]

My expected output from this would be:

101 Dalmatians
303 Fear Faith Revenge
40 Days of Night
Alice, Sweet, Alice
Battleship

I am trying the following which gets close:

getMoviesInAlpOrder() {
    const movieList = this.getMovies();
    return movieList.sort(function(a, b) {
        return a.title.toLowerCase() > b.title.toLowerCase();
    });
}

This almost works but does have some conflicts when sorting the numbers versus character titles. Any suggestions on how best to handle this type of request?

Yuschick
  • 2,642
  • 7
  • 32
  • 45
  • What is your expected output? Also, how should it sort `['200','3']`? – 4castle Jun 08 '16 at 19:37
  • The expected output should be as it currently is shown in the array. However, going forward, I can't always assume my entries will be in correct order. A title such as '200 Nights' should come before '303 Fear Faith Revenge' which should come before '40 Days of Night' and so on. – Yuschick Jun 08 '16 at 19:39
  • Updated my question to be more clear about my expected results. – Yuschick Jun 08 '16 at 19:44
  • Your current code [works fine for me](https://jsfiddle.net/278obk0r/). Can you give an example which shows the problem? – 4castle Jun 08 '16 at 19:48
  • @4castle [It doesn't](http://stackoverflow.com/q/24080785/1048572) – Bergi Jun 08 '16 at 19:54
  • "*… but does have some conflicts when sorting the numbers versus character titles.*" - Please show us an example with such a conflict. What does your current code output, and what did you expect instead? – Bergi Jun 08 '16 at 19:56
  • @Bergi I'm not saying it's good code, I'm just saying it works for their example, so they need a better one. – 4castle Jun 08 '16 at 19:57
  • @4castle: But I'm saying it's *wrong* code, and does not even work for their example depending on the sorting algorithm. – Bergi Jun 08 '16 at 20:09

3 Answers3

5

Although the other answers are correct, I think the following solution will be a bit more flexible.

Using localeCompare should solve your issue (and possibly future issues if you get text with accented characters!). Especially with movie titles, I'd expect there to be some foreign characters to sort.

getMoviesInAlpOrder() {
    const movieList = this.getMovies();
    return movieList.sort(function(a, b) {
        var lowerCaseA = a.title.toLocaleLowerCase();
        var lowerCaseB = b.title.toLocaleLowerCase();
        return lowerCaseA.localeCompare(lowerCaseB);
    });
}
Chill
  • 1,093
  • 6
  • 13
  • That looks to work. I will have to read about localeCompare to understand what this is exactly doing. Thank you! – Yuschick Jun 08 '16 at 19:55
4

You can use Array.prototype.sort():

var arr = ['Battleship','101 Dalmatians','Alice, Sweet Alice','40 Days of Night', '303 Fear Faith Revenge'];
console.log(arr.sort());
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

Simply using movieList.sort() should work. If you don't provide a compare function, it converts elements to a string and compares based on each character's unicode values. So, "10" will come before "2", and "303" will come before "40".

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Chris Wissmach
  • 505
  • 4
  • 11