Cjxcz Odjcayrwl
Cjxcz Odjcayrwl

Reputation: 22867

Sorting data in MemoryStore (or any arbitrary data array)

Does Dojo hat any utilities for sotring the data within MemoryStore, or optionally, within any data collection?

I'd need all data from the MemoryStore, but sorted by single evt. more columns. Something like Collections.sort in Java...

I'd expect Store to have sort function, but I couldn't find anything in the documentation.

Upvotes: 0

Views: 1668

Answers (2)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44725

The dojo/store API allows sorting data at query time only, as far as I know. For example:

var store = new Memory({
    data: [{
        "firstName": "Bird",
        "name": "Schultz"
    }, {
        "firstName": "Brittany",
        "name": "Berg"
    }, {
        "firstName": "Haley",
        "name": "Good"
    }, {
        "firstName": "Randolph",
        "name": "Phillips"
    }, {
        "firstName": "Bernard",
        "name": "Small"
    }, {
        "firstName": "Leslie",
        "name": "Wynn"
    }, {
        "firstName": "Mercado",
        "name": "Singleton"
    }, {
        "firstName": "Esmeralda",
        "name": "Huber"
    }, {
        "firstName": "Juanita",
        "name": "Saunders"
    }, {
        "firstName": "Beverly",
        "name": "Clemons"
    }]
});

console.log("Alphabetically by first name:");
store.query({}, {
    sort: [{
        attribute: "firstName",
        descending: false
    }]
}).forEach(function(person) {
    console.log(person.firstName + " " + person.name);
});

You can provide multiple sort attributes as well.

Full example can be found on JSFiddle: http://jsfiddle.net/9HtT3/

Upvotes: 2

MiBrock
MiBrock

Reputation: 1100

When we sort our Data, we do this actual before the Items are stored. We take the filtered Values that are saved in an array and use array.sort()and called inside a function SortByName or SortByNumbers

looks like this:

function streetsToCombobox(results){

    var adress;
    var values = [];
    var testVals={};

    var features = results.features;

    require(["dojo/_base/array","dojo/store/Memory","dijit/registry","dojo/domReady!"], function(array,Memory,registry){        
    if (!features[0]) {
        alert(noDataFound);
    }
    else {
        array.forEach(features, function(feature, i){
            adress = feature.attributes.STRASSE;

            if (!testVals[adress]) {
                testVals[adress] = true;
                values.push({
                    name: adress
                });
            }

        });
        values.sort(SortByName);

        var dataItems = {
            identifier: 'name',
            label: 'name',
            items: values
        };

            storeStreet = new Memory({
                data: dataItems
            });

 //fill existing Combobox ID,NAME,VALUE,SEARCHATTR,ONCHANGE,STORENAME,COMBOBOX  

        fillExistingCombobox(
            "adrSearchSelectCB",
            "adrSearchSelectCBName",
            "",
            "name",
            getAdresses,
            storeStreet,
            registry.byId("adrSearchSelectCBId")
        );              
    }
});
}

function SortByName(x,y) { 
return ((x.name == y.name) ? 0 : ((x.name > y.name) ? 1 : -1 ));    
}

Maybe this brings some Ideas to you how to solve your Question.

Regards, Miriam

Upvotes: 1

Related Questions