简体   繁体   中英

Sort javascript array with respect to an attribute

I am new to JSON and Javasript.

I have data in JSON

var data = [
"FirstName: 'xyz', Lastname 'QSD', rollNo:'1',EntryDate:'2012-09-11T17:35:31.835+02:00'"

"FirstName: 'abc', Lastname 'qgr', rollNo:'2',EntryDate:'2012-08-11T17:35:31.835+02:00'"

]

I want to sort it according to FirstName ,or by roll no or any other attribute i choose.

Thanks in advance.

Since you tagged the question as dojo , here is the dojo way via dojo/store/Memory . There is also a tutorial to Dojo Object Store .

See the code below in action at jsFiddle: http://jsfiddle.net/phusick/MGUBT/

require(["dojo/store/Memory"], function(Memory) {

    var data = [
        { FirstName: 'xyz', Lastname: 'QSD', rollNo: '1', EntryDate: '2012-09-11T17:35:31.835+02:00' },
        { FirstName: 'abc', Lastname: 'qgr', rollNo: '2', EntryDate: '2012-08-11T17:35:31.835+02:00' }
    ];

    var store = new Memory({ data: data });

    var sortedData = store.query(null, {
        sort:[{ attribute: "FirstName", descending: false }]
    });

    console.dir(sortedData);

});
​

If data is supposed to be an array containing objects, you could do:

data.sort(function(a,b) {
    return a.FirstName > b.FirstName;
})

The first problem is the structure of your data. You have effectively an array like

var data = [ "foo", "bar" ];

and these lines of strings contain serialized data. So first we need to extract the data via any method given in this SO question , for example the JSON library method:

var interpreted = [];
for(var i=0; i<data.length; ++i) {
    interpreted[i] = JSON.parse(data[i]);
}

Now we have structures like this:

[
    0: {
        'Firstname': 'xyz',
        'Lastname' : 'QSD', // there is a colon missing in the
                            // source, I'm guessing accidentally
        ...
    },
    1: {
        'Firstname' : 'abc',
        ...
    }
]

So we can access the firstname via interpreted[i].Firstname . Now we can sort in a similar way to this other SO question , by passing sort() a comparison function :

interpreted.sort(function(a,b) { 
    if(a.Firstname == b.Firstname)
        return 0;
    if(a.Firstname > b.Firstname)
        return 1;
    else
        return -1
} );

Where you need to swap 1 and -1 if you want to sort descending.

Your first problem is that the members are string literals, not objects. But as long as they are written as they are now, you can just use a simple sort . Just write

data.sort();

and the array will be sorted by first name.

What you want is something like:

var data = [
 {FirstName: 'xyz', Lastname 'QSD', rollNo:'1',EntryDate:'2012-09-11T17:35:31.835+02:00'},
 {FirstName: 'abc', Lastname 'qgr', rollNo:'2',EntryDate:'2012-08-11T17:35:31.835+02:00'}
]

You can then sort using the sort() function providing your own comparator like this:

data.sort(function (a, b) {
    return // return a positive number if a > b
           // use a.FirstName to access value of FirstName in a. 

})

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