Ogelami
Ogelami

Reputation: 365

Sorting DOM elements does not work properly on mobile devices(android browser, chrome, safari)

I have a div with elements inside that i would like to sort by data value.

$('div#mainFrame div.element-container').sort(function(a, b)
{
    return parseInt($(a).data('price')) < parseInt($(b).data('price'));
})
.appendTo('div#mainFrame');

I've tried it with chrome/firefox(pc) where it works perfectly fine, however it does not work with safari nor does it work with the android browser.

Here is a jsfiddle example.

Upvotes: 0

Views: 576

Answers (1)

Ogelami
Ogelami

Reputation: 365

I found a solution to the problem.

$('div#mainFrame div.element-container').sort(function(a, b)
{
    return parseInt($(b).data('price')) - parseInt($(a).data('price'));
})
.appendTo('div#mainFrame');

The reason for this is that the sort expects a number, not a boolean return value.

Upvotes: 2

Related Questions