[英]Sort javascript array with respect to an attribute
I am new to JSON and Javasript. 我是JSON和Javasript的新手。
I have data in JSON 我有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. 我想根据FirstName对其进行排序,或者按no或我选择的任何其他属性对其进行排序。
Thanks in advance. 提前致谢。
Since you tagged the question as dojo
, here is the dojo way via dojo/store/Memory
. 由于您将问题标记为
dojo
,因此这是通过dojo/store/Memory
的dojo方法。 There is also a tutorial to Dojo Object Store . 还有一个Dojo Object Store教程。
See the code below in action at jsFiddle: http://jsfiddle.net/phusick/MGUBT/ 请参阅下面在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
应该是包含对象的数组,则可以执行以下操作:
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:
因此,首先我们需要通过此SO问题中给出的任何方法(例如JSON库方法)提取数据:
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
. 因此,我们可以通过
interpreted[i].Firstname
访问名字。 Now we can sort in a similar way to this other SO question , by passing sort()
a comparison function : 现在,我们可以通过传递
sort()
一个比较函数 ,以与其他SO问题类似的方式进行sort()
:
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. 如果要降序排序,则需要在其中交换1和-1。
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 .
但是,只要按照现在的方式编写它们,就可以使用简单的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: 然后,您可以使用sort()函数进行排序,以提供自己的比较器,如下所示:
data.sort(function (a, b) {
return // return a positive number if a > b
// use a.FirstName to access value of FirstName in a.
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.