admin管理员组文章数量:1435859
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.
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.
Share Improve this question edited Oct 1, 2012 at 9:47 Zlatko 19.6k14 gold badges74 silver badges129 bronze badges asked Oct 1, 2012 at 9:29 GameBuilderGameBuilder 1,1895 gold badges32 silver badges64 bronze badges 7- 2 That isn't valid JSON. It isn't even valid JavaScript. – Quentin Commented Oct 1, 2012 at 9:36
- @Quentin : Fine, I have this DATA and i want to sort by first Name , or roll no using Java Script – GameBuilder Commented Oct 1, 2012 at 9:46
- It's not really a JSON either, it's an array (and an incorrect one at that, it lacks a ma at the end of the first row). JSON would look like: var data = { rows: [ {"firstname": "joe", "id": 1}, {"firstname": "jack", "id": 2}]}; – Zlatko Commented Oct 1, 2012 at 9:49
- @GameBuilder — You can either write a custom parser for it (which is somewhat too large a problem to walk through all the steps of in a StackOverflow answer) or you can fix the data. – Quentin Commented Oct 1, 2012 at 9:50
- Or see my answer. With a bit of formatting the strings can be parsed as JSON (it's very close, despite the criticisms above), and then sorted with a parison function. – Phil H Commented Oct 1, 2012 at 9:52
4 Answers
Reset to default 4Since 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/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);
});
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 parison 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.
If data
is supposed to be an array containing objects, you could do:
data.sort(function(a,b) {
return a.FirstName > b.FirstName;
})
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 parator 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.
})
本文标签: jsonSort javascript array with respect to an attributeStack Overflow
版权声明:本文标题:json - Sort javascript array with respect to an attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675657a2669836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论