user3686908
user3686908

Reputation: 23

My JavaScript doesn't work with chrome

I have a javascript which work correctly in Firefox, but it doesn't work in chrome! The code is two array list that is supposed to sort a list of movies in alphabetical order. One list from A to Z and one list with Z to A.

Any ideas what i have done wrong? Thanks alot

var Movies  =  ["Pulp Fiction", "The Dark Knight", "Fight Club", " Terminator", "Matrix", "American History X", "Memento "];



function listMovies()
{
    document.writeln("<b>Movies sort from A to B:</b>");                            
    document.writeln(Movies.sort().join("<br>"));

    document.writeln("<br><b>Movies sort from B to A:</b>");                        
    document.writeln(Movies.sort(function(a, b){return b-a}).join("<br>"));
}
listMovies();

Upvotes: 2

Views: 598

Answers (3)

W.D.
W.D.

Reputation: 1041

What about this DEMO ?

var Movies  =  ["Pulp Fiction", "The Dark Knight", "Fight Club", " Terminator", "Matrix", "American History X", "Memento "];



function listMovies(){

  for(var i=0;i<Movies.length;i++){

    Movies[i] = Movies[i].trim();    

  }

    document.writeln("<b>Movies sort from A to B:</b>");                            
    document.writeln(Movies.sort().join("<br>"));

    document.writeln("<br><b>Movies sort from B to A:</b>");                        
    document.writeln(Movies.sort(function(a, b){return b-a}).join("<br>"));

    }

listMovies();

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074028

Two issues:

  1. You have a space in front of Terminator which is throwing off the results.

  2. Your "B to A" function is using the - operator on strings, which is not going to give you reliable results.

To solve #2, use a function that doesn't try to subtract strings:

document.writeln(Movies.sort(function(a, b){
    return a == b ? 0 : (a < b ? 1 : -1)
}).join("<br>"));

Live Example


Side note: I'd avoid using document.writeln and similar.

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237817

First, you probably shouldn't be using document.writeLn. It's almost never necessary. Use DOM methods instead.

The problem, however, is that your code is trying to subtract one string from another in calculating the relative positions:

Movies.sort(function(a, b){return b-a})

This works fine with numbers (4 - 2 === 2) but not with strings ('Terminator' - 'American History' is NaN).

You need to compare with < and > instead, which makes your function a little more complex:

function listMovies()
{
    document.writeLn("<b>Movies sort from A to B:</b>");                            
    document.writeLn(Movies.sort().join("<br>"));

    document.writeLn("<br><b>Movies sort from B to A:</b>");                        
    document.writeLn(Movies.sort(function(a, b){
        if (b === a) return 0;
        if (b > a) return 1;
        return -1;
    }).join("<br>"));
}

The other oddity you have is that Terminator has a leading space, which causes it to go the beginning of the list alphabetically.

Upvotes: 1

Related Questions