简体   繁体   中英

WebSQL/SQLite order by javascript date obj

I'm working on a html5/jqm/phonegap application. I'm selecting a list of records from a client-side db table. I want to group and order them by date but the date that exists in the table is a JS date object like:

Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)

I want to group and order them by the numerical day (18,19,20 in this example). So the following details:

Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Tue Nov 12 2012 16:20:55 GMT+0000 (GMT)

Would be returned in this order:

Tue Nov 20 2012 16:20:55 GMT+0000 (GMT)
Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)

Is it possible to do this in sql as part of the select? Or if not could someone piont me in the right direction on how to do this within Js/jQuery?

Thanks in advance

** Edit in response to questions from Pete **

I'm using the Jaascript/html5 client side version of SQL, which I believe is based on SQLite. To create the DB I'm just calling the following js -

var db = openDatabase(shortName, version, displayName, maxSize);

My Db schema is :

transaction.executeSql('CREATE TABLE RECORDINGS (id INTEGER PRIMARY KEY AUTOINCREMENT, caseRef, startDate, endDate, caseTypeId, actionTypeId, notes, userId, syncStatus);');
transaction.executeSql('INSERT INTO RECORDINGS (caseRef, startDate, endDate, caseTypeId, actionTypeId, notes, userId, syncStatus) VALUES ("CR1234", "'+startDate+'", "'+endDate+'", 1, 2, "Meeting about something", 1, 0);');

My select currently looks like:

function getRecordingsQuery(transaction) {
        transaction.executeSql('SELECT RECORDINGS.id, RECORDINGS.caseRef, RECORDINGS.startDate, RECORDINGS.endDate, RECORDINGS.notes, RECORDINGS.syncStatus, CASETYPES.description FROM RECORDINGS, CASETYPES WHERE RECORDINGS.caseTypeId = CASETYPES.id AND RECORDINGS.userId =? ORDER BY startDate DESC', [window.currentSignedInUserId], getRecordingsDataHandler, errorHandler);
    }

Does that help?

UPDATE

SQLLite does not support DATEPART , but it does support strftime . Try adding the following as your ORDER BY :

ORDER BY CAST(strftime("%d", date) AS INTEGER) DESC

You could instead process the sort in JavaScript. I'm sure there are better, or more efficient ways to do this, but you could just use the built-in sort for arrays like so:

var getRecordingsDataHandler = function getRecordingsDataHandler(tx, results) {
    var rows = [],
        row = null,
        i = 0;
      if (results.rows && results.rows.length) {
        for (i = 0; i < results.rows.length; i += 1) {
            rows.push({
                "startDate": new Date(results.rows.item(i).startDate),
                "data": results.rows.item(i)
            });
        }
        rows.sort(function (a, b) {
            var ascending = a.startDate.getDate() - b.startDate.getDate(),
                descending = b.startDate.getDate() - a.startDate.getDate();
            return descending;
        });
        for (i = 0; i < rows.length; i += 1) {
            row = rows[i].data;
            //process the rest
            //since row === results.rows.item(i), reference from row
        }
    }
};

ORIGINAL:

Without knowing what SQL version you are using, it's harder to answer that portion of your question. I think most of them support a DATEPART function, so you could try:

ORDER BY DATEPART(dd, YourDateField) DESC

As far as JavaScript/jQuery:

var d = new Date('Tue Nov 20 2012 16:20:55 GMT+0000 (GMT)');

will return a date object in JavaScript which you can then sort by. For just the date:

d.getDate(); //returns day of month

If you can be a little more specific in your question (which engine/version of SQL you are using, or how your client-side table is structured and the function you are using to sort it), then we may be able to assist you a little better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM