2

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();
3
  • 3
    Works perfectly fine in my Chrome, but I would avoid using writeln Commented May 31, 2014 at 11:51
  • 1
    document.writeln(Movies.sort(function(a, b){return b-a}).join("<br>")); missing ; in function(a, b){return b-a}) add and try. Commented May 31, 2014 at 11:52
  • 1
    @DeepMehta: The semicolon is optional, thanks to the horror that is "automatic semicolon insertion." It has nothing to do with the problem. Commented May 31, 2014 at 11:59

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

about document.writeln, this is the way been learning in this course...? maybe it's a bad updated course
@user3686908: Good deal. If this answered your question, don't forget to click the checkmark to "accept" the answer. Re document.writeln, yeah, I'd find another tutorial. FWIW, here's a minimal mod to use a more modern approach: jsbin.com/lucerulu/2/edit
ok! How can i give rep to users who helped? Also, can i only accept 1 answer? Thx
@user3686908: Right, you can only accept one answer. When your rep is over 15, you'll also be able to upvote answers that were useful. So at that point, typically you'd upvote and accept the answer that's "best" in your view, and upvote any others that you felt were helpful. (You're close, as I write this your rep is 13, so...)
1

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.

Comments

0

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

1 Comment

I meant - is this the expected result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.