timmyg
timmyg

Reputation: 181

Isotope Not Sorting

I am using isotope v1.5.24 and jQuery in no-conflict mode, and trying to sort the data by a number but it is not working.

I've set up a jsfiddle here: http://jsfiddle.net/tgelles/esxw3kne/1/, and you can see the first item should be third in the list.

The isotope code is:

var $j = jQuery.noConflict();
$j(document).ready(function () {
    //set isotope variables
    var $container = $j('.section-container');
    filters = {};

    $container.isotope({
        getSortData: {
            course_number: function ($elem) {
                return parseInt($elem.find('.course-number').text(), 10);
            }
        },
        itemSelector: 'section',
        layoutMode: 'straightDown'
    });
    $container.isotope({
        sortBy: 'course_number'
    });

FWIW the 'course_number' sortBy is actually json decoded from an API, which in the fiddle is simple HTML.

Upvotes: 3

Views: 842

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24661

It looks like you're calling parseInt on a string such as this: 'AS.010.232' According to this, parseInt will return NaN for such values. Quoting from there:

If the first character cannot be converted to a number, parseInt returns NaN.

Since all of those sort values are being interpreted as NaN it may actually be sorting them, just in some random order.

Instead of calling parseInt, try just returning the text directly:

getSortData: {
    course_number: function ($elem) {
        return $elem.find('.course-number').text();
    }
},

This seems to be working in this updated fiddle, and is also pretty much identical to the examples on Isotope's manual entry for sorting.

Upvotes: 1

Related Questions