使用sortBy方法排序无法正常工作

时间:2017-05-13 04:55:04

标签: javascript underscore.js

  

我正在尝试使用下划线sortBy方法对对象数组进行排序,json如下

[
   {
      "styleId": 91,
      "styleName": "Style Label",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 1,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": true,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "real",
      "createdDate": 1494217733790,
      "modifiedBy": "superuser",
      "modifiedDate": 1494388952450,
      "rowVersion": "AAAAAAIC8ls="
   },
   {
      "styleId": 69,
      "styleName": "irtest",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 16,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": false,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": false,
      "styleDesign": null,
      "createdBy": "arun",
      "createdDate": 1493288218843,
      "modifiedBy": "real",
      "modifiedDate": 1494062410107,
      "rowVersion": "AAAAAAHZaDk="
   },
   {
      "styleId": 21,
      "styleName": "new style-copy",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 5,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": false,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "Nandita",
      "createdDate": 1493186162607,
      "modifiedBy": "Nandita",
      "modifiedDate": 1493186173807,
      "rowVersion": "AAAAAAEXEwI="
   },
   {
      "styleId": 97,
      "styleName": "style 1",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 1,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": true,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "real",
      "createdDate": 1494646737753,
      "modifiedBy": "real",
      "modifiedDate": 1494647127567,
      "rowVersion": "AAAAAAIxLKM="
   }
]

  

这是我的样本json我试图通过字段styleName像``

对这个json进行排序
var result = _.sortBy(ctrl.styles, function (o) { return o.styleName; });
  

结果如下

[
   {
      "styleId": 21,
      "styleName": "new style-copy",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 5,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": false,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "Nandita",
      "createdDate": 1493186162607,
      "modifiedBy": "Nandita",
      "modifiedDate": 1493186173807,
      "rowVersion": "AAAAAAEXEwI="
   },
   {
      "styleId": 69,
      "styleName": "irtest",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 16,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": false,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": false,
      "styleDesign": null,
      "createdBy": "arun",
      "createdDate": 1493288218843,
      "modifiedBy": "real",
      "modifiedDate": 1494062410107,
      "rowVersion": "AAAAAAHZaDk="
   },
   {
      "styleId": 91,
      "styleName": "Style Label",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 1,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": true,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "real",
      "createdDate": 1494217733790,
      "modifiedBy": "superuser",
      "modifiedDate": 1494388952450,
      "rowVersion": "AAAAAAIC8ls="
   },
   {
      "styleId": 97,
      "styleName": "style 1",
      "dataSourceId": 1,
      "dataSourceName": null,
      "stationeryId": 1,
      "stationeryName": null,
      "styleType": 1,
      "styleTypeName": "Labels",
      "isActive": true,
      "isPlainPaper": true,
      "isSystemMaintained": false,
      "isPublished": true,
      "designerUrl": null,
      "sourceStyleId": 0,
      "rowIndicatorCode": 2,
      "isDefault": null,
      "hasElement": true,
      "styleDesign": null,
      "createdBy": "real",
      "createdDate": 1494646737753,
      "modifiedBy": "real",
      "modifiedDate": 1494647127567,
      "rowVersion": "AAAAAAIxLKM="
   }
]

  

其中ctrl.styles是上面的JSON,有什么问题吗?

2 个答案:

答案 0 :(得分:3)

var result = _.sortBy(ctrl.styles,function(a){return a.styleName.toUpperCase();});

  

我遇到的问题是styleName,其中有不同的case,将styleName转换为大写可以解决问题。 Yashar的回答也解决了这个问题

答案 1 :(得分:2)

使用this link并更正返回值后,我发现这对我有用:(我将数组命名为arr,而不是ctrl.styles

var res = _.sortBy(arr, function (a) {
    var x=  _.map(a.styleName.split(''), function (i) {
        return i.charCodeAt(0);
    }).join('');
    return x;
});

或@ArunBabuVijayanath在评论中说:

var res = _.sortBy(arr, function (a) {
    return a.styleName.toUpperCase();
});