knadh / localStorageDB

A simple database layer for localStorage and sessionStorage for creating structured data in the form of databases and tables
http://nadh.in/code/localstoragedb
814 stars 128 forks source link

Any chance to implement sort method? #18

Closed juarezpaf closed 10 years ago

juarezpaf commented 11 years ago

Hey @knadh Congratulations for this awesome project, I am wondering if it is possible to implement a sort method. Thanks in advance!

knadh commented 11 years ago

Hey, sorry for the delayed reply. Let me look into it implementing a sort method.

juarezpaf commented 11 years ago

It will be great for sure! :smile:

a904guy commented 10 years ago

I started implementation but quit as it no longer fit my needs. Feel free to pick it up. @ (https://github.com/a904guy/localStorageDB/blob/master/localstoragedb.js)

@line: 166

// sort records by field name (sort) returned from select
function sort(results,sort)
{
    var r = [], sortable, s = sort;
    for(var record in results)
    {
        record.sort(function(a,b)
            { 
                if (typeof a[s].localeCompare === 'function') return a[s].localeCompare(b[s]);
                return a[s] - b[s];
            }
        );
        r.push(record);
    }
    return r;
}

@line: 619:

    // select rows
    query: function(table_name, query, limit, start, sort) {
        tableExistsWarn(table_name);

        var result_ids = [];
        if(!query) {
            result_ids = getIDs(table_name, limit, start); // no conditions given, return all records
        } else if(typeof query == 'object') {           // the query has key-value pairs provided
            result_ids = queryByValues(table_name, validFields(table_name, query), limit, start);
        } else if(typeof query == 'function') {     // the query has a conditional map function provided
            result_ids = queryByFunction(table_name, query, limit, start);
        }
        return sort(select(table_name, result_ids, limit),sort);
    },
orangecoding commented 10 years ago

@a904guy see my PullReq. https://github.com/knadh/localStorageDB/pull/35

knadh commented 10 years ago

@juarezpaf, sorry it's been so long, but the multi-field sorting is now supported (https://github.com/knadh/localStorageDB/commit/63a17494ebe3e22f442f43fc25d432d52098afb1). Thanks to @orangecoding.