1

Is working fine on Chrome, could someone tell me why it isn't sorting the table on Safari and how to fix it?
I've looked the console, there's no error.

HTML:

        <tr>
            <td>
                <input name="d1" value="01/01/1992">   
            </td>
        </tr>

        <tr>
            <td>
                <input name="d1" value="01/01/1991">   
            </td>
        </tr>
    </tbody>
</table>
<button>SORT</button>


jQuery:

$('button').on('click',function(){
    sort();
});

function sort() {
    $('tBody tr').sort(function(a, b) {
        return new Date($(a).find('input[name="d1"]').val()).getTime() > new Date($(b).find('input[name="d1"]').val()).getTime()
    }).appendTo('tBody');
}

JsFiddle:
http://jsfiddle.net/nm5vbtdq/1/

j08691
  • 204,283
  • 31
  • 260
  • 272
BernardoLima
  • 1,103
  • 2
  • 16
  • 35
  • What kind of error do you get in Safari? – putvande Dec 05 '14 at 16:54
  • It doesn't sort, there's no error on console. – BernardoLima Dec 05 '14 at 16:55
  • Have you checked that `new Date(...)` produces the same values for your input strings in all browsers? Unless you're using standard ISO `YYYY-MM-DDTHH:mm:ss.sssZ` strings, it is quite likely they are not being parsed the same. – apsillers Dec 05 '14 at 16:55
  • @apsillers I didn't know that, I'm using format mm/dd/yyyy, must be that, I'll search how to convert it. – BernardoLima Dec 05 '14 at 16:55
  • 1
    You may be interested in the [moment.js](http://momentjs.com/) library, which allows you to specify a format string, rather than relying on implementation-variable support. – apsillers Dec 05 '14 at 16:57

1 Answers1

4

I believe you need to return a -1/1 instead of a boolean in Safari, see below:

function sort() {
    $('tBody tr').sort(function(a, b) {
        var result = new Date($(a).find('input[name="d1"]').val()).getTime() > new Date($(b).find('input[name="d1"]').val()).getTime() ? 1 : -1;
        return result;
    }).appendTo('tBody');
}
sbonkosky
  • 2,537
  • 1
  • 22
  • 31