简体   繁体   English

根据属性对javascript数组进行排序

[英]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.

相关问题 按属性对对象JavaScript数组进行排序 - Sort array of object javascript by attribute 按对象属性对javascript数组排序 - sort javascript array by object attribute 如何根据数组属性的索引拆分和合并 javascript object - How to split and merge javascript object with respect to index of array attribute Javascript:排序数组并返回一个索引数组,指示排序元素相对于原始元素的 position - Javascript: Sort array and return an array of indices that indicates the position of the sorted elements with respect to the original elements 根据一个属性的值和日期 object 对数组进行排序,以使用 javascript 获取最近的条目 - Sort an array with respect to value of one property and date object to fetch recent entry using javascript 按特定属性值对数组进行排序,并在 JavaScript 中将其优先排序到数组的顶部 - sort array by specific attribute value and prioritize it to the top of array in JavaScript 如何基于确定的属性值对JavaScript中的对象数组进行排序? - How to sort array of objects in JavaScript based on definite attribute value? 帮助将 DIV 中的属性数据排序捕获为数组? jQuery/Javascript - Help capturing attribute data-sort in a DIV as an array? Jquery/Javascript 按属性对 JavaScript 对象进行排序 - Sort JavaScript Object by Attribute 相对于 javascript 中的第一个数组对第二个数组进行排序 - Sorting second array with respect to first array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM