jquery排序json对象

发布于 2024-12-11 03:40:31 字数 1215 浏览 0 评论 0原文

我在 jquery 中有下面的 Json 对象,我用它来填充 jquery 网格。

现在我需要根据“performanceName”对其进行排序。

请建议我如何实现这一目标。请我知道同样的问题已发布 100 次,但我仍然努力在下面的 json 上实现排序

{
    "callback": "",
    "html": "",
    "message": [
        ""
    ],
    "responseData": [
        {
            "collectionTitleId": 1107861,
            "collectionTitleIdSpecified": true,
            "performanceField": {
                "addedDate": "6/16/2011 11:11:10 PM",
                "artist": "Corbis",
                "performanceName": "Showcase Graphic 003 - Anime Girl Purple",
                "performanceType": "Graphic",
                "provider": "DMSP Dynamic Digital"
            },
            "sortOrder": "01"
        },
        {
            "collectionTitleId": 1113513,
            "collectionTitleIdSpecified": true,
            "performanceField": {
                "addedDate": "5/25/2011 9:27:39 AM",
                "artist": "Beloved",
                "performanceName": "Hating On Your Feet (Verse)",
                "performanceType": "LabelTone",
                "provider": "p1"
            },
            "sortOrder": "02"
        }
    ],
    "status": [
        "Success"
    ]
}

提前致谢

I have the below Json object in jquery which i m using to fill a jquery grid.

Now i need to sort it on the basis of "performanceName".

Please advice how i can achieve this. Please i knw same kinda question is posted 100 times but still i m struggling to implement sorting on below json

{
    "callback": "",
    "html": "",
    "message": [
        ""
    ],
    "responseData": [
        {
            "collectionTitleId": 1107861,
            "collectionTitleIdSpecified": true,
            "performanceField": {
                "addedDate": "6/16/2011 11:11:10 PM",
                "artist": "Corbis",
                "performanceName": "Showcase Graphic 003 - Anime Girl Purple",
                "performanceType": "Graphic",
                "provider": "DMSP Dynamic Digital"
            },
            "sortOrder": "01"
        },
        {
            "collectionTitleId": 1113513,
            "collectionTitleIdSpecified": true,
            "performanceField": {
                "addedDate": "5/25/2011 9:27:39 AM",
                "artist": "Beloved",
                "performanceName": "Hating On Your Feet (Verse)",
                "performanceType": "LabelTone",
                "provider": "p1"
            },
            "sortOrder": "02"
        }
    ],
    "status": [
        "Success"
    ]
}

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-12-18 03:40:31

您可以使用 Array.prototype.sort 就地对数组进行排序。如果没有任何参数,这会尝试按字母顺序对元素进行排序,但您可以传入比较函数。该函数接收两个参数,并应返回小于 0、0 或大于 0 的值,以定义参数 1 与参数 2 的关系。

您的排序函数应如下所示:

data.responseData.sort(function (a, b) {
    a = a.performanceField.performanceName,
    b = b.performanceField.performanceName;

    return a.localeCompare(b);
});

工作演示:http://jsfiddle.net/AndyE/aC5z4/

localeCompare< /code> 为我们完成了比较字符串的艰苦工作。

You can use Array.prototype.sort to in-place sort an array. Without any arguments, this attempts to sort elements alphabetically, but you can pass in a comparing function instead. This function receives two arguments and should return less than 0, 0 or greater than 0 to define where argument 1 should be in relation to argument 2.

Your sorting function should look something like this:

data.responseData.sort(function (a, b) {
    a = a.performanceField.performanceName,
    b = b.performanceField.performanceName;

    return a.localeCompare(b);
});

Working demo: http://jsfiddle.net/AndyE/aC5z4/

localeCompare does the hard work of comparing the strings for us.

恍梦境° 2024-12-18 03:40:31
var people = [
    { 'myKey': 'Ankit', 'status': 0 },    
    { 'myKey': 'Bhavik', 'status': 3 },
    { 'myKey': 'Parth', 'status': 7 },
    { 'myKey': 'Amin', 'status': 9 },
    { 'myKey': 'Russ', 'status': 9 },
    { 'myKey': 'Pete', 'status': 10 },
    { 'myKey': 'Ravi', 'status': 2 },
    { 'myKey': 'Tejas', 'status': 2 },
    { 'myKey': 'Dilip', 'status': 1 },
    { 'myKey': 'Piyush', 'status': 12 }
];
alert("0. At start: " + JSON.stringify(people));

//META.fn: sortData
jQuery.fn.sortData = function (prop, asc) {
        return this.sort(function (a, b) {           
            var x = a[prop];
        var y = b[prop];
            var retrunStatus = ((x < y) ? 1 : ((x > y) ? -1 : 0));
            return (asc==undefined || asc) ? (retrunStatus * -1) : retrunStatus ;        
        });
    }

people2 = $(people).sortData('myKey',false);
alert("1. After sorting (0 to x): " + JSON.stringify(people2));

var people = [
    { 'myKey': 'Ankit', 'status': 0 },    
    { 'myKey': 'Bhavik', 'status': 3 },
    { 'myKey': 'Parth', 'status': 7 },
    { 'myKey': 'Amin', 'status': 9 },
    { 'myKey': 'Russ', 'status': 9 },
    { 'myKey': 'Pete', 'status': 10 },
    { 'myKey': 'Ravi', 'status': 2 },
    { 'myKey': 'Tejas', 'status': 2 },
    { 'myKey': 'Dilip', 'status': 1 },
    { 'myKey': 'Piyush', 'status': 12 }
];
alert("0. At start: " + JSON.stringify(people));

//META.fn: sortData
jQuery.fn.sortData = function (prop, asc) {
        return this.sort(function (a, b) {           
            var x = a[prop];
        var y = b[prop];
            var retrunStatus = ((x < y) ? 1 : ((x > y) ? -1 : 0));
            return (asc==undefined || asc) ? (retrunStatus * -1) : retrunStatus ;        
        });
    }

people2 = $(people).sortData('myKey',false);
alert("1. After sorting (0 to x): " + JSON.stringify(people2));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文