how to sort by date (new) with jquery isotope

后端 未结 2 1186
遇见更好的自我
遇见更好的自我 2021-01-02 09:28

Forgive me as I\'m a bit novice to JS, just know enough to manipulate.

For those familiar with Isotope ( http://isotope.metafizzy.co/docs/sorting.html ), I have a b

相关标签:
2条回答
  • 2021-01-02 09:36

    Heres what I did to sort by date.

    If your date is in the format of '01/01/2012' then you need to convert it to a javascript date object.

    getSortData: {
        date: function ($elem) {
            var dateStr = $elem.find('.date').text(),
                dateArray = dateStr.split('/'),
                year = dateArray[2],
                month = dateArray[0],
                day = dateArray[1];
            return new Date(year, month, day);
        }
    }
    

    then you do your usual

    $('#container').isotope({ sortBy: 'date' });
    
    0 讨论(0)
  • 2021-01-02 09:56

    Here is how I did it, jsFiddle

    $container.isotope({
        itemSelector: '.element',
        getSortData: {
            date: function ($elem) {
                return Date.parse($elem.find('.date').text());
            }
        }
    });
    

    It doesn't seem to matter what format the date is in, and works ascending and descending.

    The parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970.

    Basically it turns your date strings into nice solid numbers which are easy to compare.

    0 讨论(0)
提交回复
热议问题