3

I am trying to sort an array of objects using underscore sortBy method the json is as follows

[
   {
      "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="
   }
]

This is my sample json I am trying to sort this json by the field styleName like`

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

Result am getting is as follows

[
   {
      "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="
   }
]

where ctrl.styles is the above JSON, Is there something am doing wrong?

3
  • What output are you expecting and what output are you getting? Commented May 13, 2017 at 5:04
  • I have updated the question with the result that am currently having, expected result will be sorted array by styleName where styleName starting with 'a' at the start and 'z' at the end Commented May 13, 2017 at 5:16
  • When I paste your array into dev tools on underscore's web site and sort using your code it seems to return the correct result. Commented May 13, 2017 at 5:24

2 Answers 2

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

The problem I was facing was the styleName where having different case, converting styleName to uppercase solved the problem. Yashar's answer also solved the issue

Sign up to request clarification or add additional context in comments.

Comments

2

With using this link and correcting return value, I found this works for me:(I suppresed array and named it as arr instead of ctrl.styles)

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

or as @ArunBabuVijayanath said in comments:

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

1 Comment

A better solution would be var result = _.sortBy(ctrl.styleName, function (a) { return a.styleName.toUpperCase(); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.