Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Problem using Array sort in phantomJS #11090

Closed
thanpolas opened this issue Feb 23, 2013 · 8 comments
Closed

Problem using Array sort in phantomJS #11090

thanpolas opened this issue Feb 23, 2013 · 8 comments

Comments

@thanpolas
Copy link

Thanpolas@gmail.com commented:

I've been trying to make a few qunit tests pass using phantomJS but i had problems.

All qunit tests pass on the browser but fail using phantomJS. After investigation I noticed that an array failed to get sorted as expected. It appears that Array.prototype.sort.call() will not perform as expected on phantomJS.

This is the test case i used to replicate the issue:

var arNum = [2, 4, 1];


var arObj = [
  {name: 'two', val: 20},
  {name: 'three', val: 30},
  {name: 'one', val: 10}
];


function sortObj(a, b) { return a.val < b.val; }
function sortNum(a, b) { return a < b; }


Array.prototype.sort.call(arObj, sortObj);
Array.prototype.sort.call(arNum, sortNum);


equal(arNum[1], 2, 'Array with numbers, second item should be num 2');
equal(arObj[1].name, 'two', 'Array with Objects, second item should be "two"');

The complete document with the tests can be found in this gist: https://gist.github.com/thanpolas/5014065

Disclaimer:
This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #1090.
🌟   3 people had starred this issue at the time of migration.

@thanpolas
Copy link
Author

Thanpolas@gmail.com commented:

Ignore this issue, it was my mistake for returning a boolean instead of -1, 0 or 1 in the sorting functions.

@ariya
Copy link
Owner

ariya commented Mar 4, 2013

ariya.hi...@gmail.com commented:

 

 
Metadata Updates

  • Status updated: Invalid

@ariya ariya closed this as completed Mar 4, 2013
@esposito
Copy link

I have a problem seemingly related to the issue described above. Whenever I sort an Array in phantomJS using a sort callback of the form function(a, b) { return a - b }, the array gets sorted in reverse order. When I run my code in a regular browser (like Chrome), everything works fine. I first encountered this problem in august 2012. With the latest version of phantomJS (1.9.1) the problem remains.

Hopefully someone can point me in the right direction.

@esposito
Copy link

After some testing I found out that although the meaning of the values 1 and -1 in a sorting callback in phantomJS is opposite to their meaning in Chrome in that context, the arguments are also passed in in reverse order, so that the final result is the same. My problem was caused by the fact that Chrome happily executes new Date('2012-1-12'), whereas phantomJS returns Invalid Date, making the value end up at the end of a sorted Array.

@zanona
Copy link

zanona commented Aug 2, 2013

#bump it indeed happens when any true becomes false, including the first example mentioned on

var a = [
        { name: 'foo', index: 4 },
        { name: 'bar', index: 2 },
        { name: 'fooz', index: 3 },
        { name: 'baz', index: 0 }
    ].sort(function (a, b) {
        var r = a.index > b.index;;
        console.log(r);
        return r;
    });
    console.log(a.map(function (tag) { return tag.index}));

Where I guess a and b are inverted on phantomjs

phantomjs output

false
false
true
false
false
4,2,3,0

v8 output

true
true
false
true
true
true
[ 0, 2, 3, 4 ]

@kcrwfrd
Copy link

kcrwfrd commented May 16, 2014

Thank you for the clues, @esposito and @zanona. We had a sort like this:

arr = ['foo', 'bar', 'baz'];

arr.sort(function(a) {
  // Move 'foo' to the end of the array
  if (a == 'foo') return 1;

  return 0;
}

PhantomJS was sorting it incorrectly because it inverts the a and b. Revising it to the following worked:

arr.sort(function(a, b) {
  if (a == 'foo') return 1;
  if (b == 'foo') return -1;

  return 0
});

I would be inclined to still consider this an open bug in PhantomJS. Why sort in reverse order?

@JDvorak
Copy link

JDvorak commented Sep 3, 2014

How is this closed?

@JamesMGreene
Copy link
Collaborator

This was closed over a year ago because the original submitter said it was his mistake. If there is an additional problem, please open a new issue as closed ones aren't tracked.

Finally, this is likely already fixed in PhantomJS 2.0 (unstable master branch), in which case this [or a new] issue would be rightfully closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants