Emanuela colta Emanuela colta - 11 months ago 43
Node.js Question

Order by date a list with multiple dates

I am building a page where I display a list of events. Each of the events takes place in one or two consecutive dates. The issue is that these events should be order by these dates.
I use Sequelize.js.

For example:
event1 - in 13th and 14th of July should be displayed before the event2- in 10th and 11th of July because it the most recent one.

I have two tables: Events and EventDates with a one-to-many relationship.

Event.findAll({
include: [
{ model:EventDates, order: [ [ 'date', 'DESC' ]] },
],

})


This will only order the two dates of one event, but will not compare the dates of different events.

How can I perform the operation of order?

EventDates model:

var EventDates= sequelize.define('eventdates', {
date: {
type: Sequelize.DATE
},
availabletickets: {
type: Sequelize.INTEGER
},
},
{
freezeTableName: true
});


And the Events model:

var Event = sequelize.define('event', {
title: {
type: Sequelize.STRING,
},
slug: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
isDeleted:{
type: Sequelize. BOOLEAN,
defaultValue: false
},

}
});

Answer Source

In your current code your are ordering only inside Eventdates, you have to to order the whole list too:

Event.findAll({ 
        include: [
            { model:EventDates, order: [ [ 'date', 'DESC' ]] },                  
        ],
        order: [[ EventDates , 'date', 'DESC']]

    })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download