skOllie Posted May 9, 2015 Share Posted May 9, 2015 Hi: i come from china. If my English is not good, hope you can understand. i am using Google translation I was confused about the example on chapter 7.2.2. About function sort(). The book says: var numbers = [1, 4, 3, 2]; numbers.sort(); //4, 1, 3, 2 why it ordered 4, 1, 3, 2? I test this code and it always to be 1, 2 ,3 ,4 .even IE 6. thank you. Link to comment Share on other sites More sharing options...
Bill Posted May 11, 2015 Share Posted May 11, 2015 It's a typing error! Rafal says regarding errata on this webstie that on Page 247:var numbers = [1, 4, 3, 2];numbers.sort(); // 4, 1, 3, 2"For me it looks the better example will be [1, 14, 3, 2] and in old browsers the result will be 1, 14, 2, 3." See link: http://www.larryullman.com/books/modern-javascript-develop-and-design/errata/comment-page-1/ Link to comment Share on other sites More sharing options...
Bill Posted May 11, 2015 Share Posted May 11, 2015 Sorting appears to be a problem with early browsers but with use of function x-y problem has been resolved with modern browsers. Click on link for more detailed info on numbers.sort() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2Fsort Link to comment Share on other sites More sharing options...
Recommended Posts