Sorting the results of an indexedDB query

indexeddb, javascript

Solution

Thanks to zomg, hughfdjackson of javascript irc, I sorted the final array. Modified code as below:

var trans = db.transaction(['msgs'], IDBTransaction.READ);
var store = trans.objectStore('msgs');

// Get everything in the store;
var keyRange = IDBKeyRange.lowerBound("");
var cursorRequest = store.openCursor(keyRange);

var res = new Array();

cursorRequest.onsuccess = function(e) {
    var result = e.target.result;
    if(!!result == false){
        **res.sort(function(a,b){return Number(a.date) - Number(b.date);});**
        //print res etc....
        return;
    }
    res.push(result.value);
    result.continue();
};

Problem

I want to sort results obtained from indexedDB. Each record has structure {id, text, date} where 'id' is the keyPath. I want to sort the results by date. My current code is as below: ``` var trans = db.transaction(['msgs'], IDBTransaction.READ); var store = trans.objectStore('msgs'); // Get everything in the store; var keyRange = IDBKeyRange.lowerBound(""); var cursorRequest = store.openCursor(keyRange); cursorRequest.onsuccess = function(e) { var result = e.target.result; if(!!result == false){ return; } console.log(result.value); result.continue(); }; ```

Original source

Related problems