1

Kindly help me in sorting the below JSON list in the controller and display in the view.

Actually orderBy filter sorting one level, But I need it sort even for childs recursively.

Input:

R2
-->S4
------>T5
------>T4
-->S3
R1
-->S2
------>T2
------>T1
-->S1

Output:

R1
-->S1
------>T1
------>T2
-->S2
R2
-->S3
------>T4
------>T5
-->S4

Please find the sample in Plunker.

http://plnkr.co/edit/JslHwJ1CBREKf6FgYHaZ?p=preview

3
  • You should create a custom filter for this. Commented Apr 16, 2014 at 17:54
  • Oh OK. I will create the custom filter. Could you please suggest how I can create the algorithm for this. Commented Apr 16, 2014 at 18:00
  • Sorry but I can't help you with that. Commented Apr 16, 2014 at 18:01

1 Answer 1

1
var sortNames = function(arr){

  arr = arr.sort(function(a,b){
    return a.name > b.name;
  })

  for (var i = 0; i < arr.length; i++){
    if (arr[i].childs){
      sortNames(arr[i].childs)
    }
  }
  return arr;
}
var names = [
    {
        "name": "Root1",
        "Id": "2f3d17cb-d9e2-4e99-882d-546767f2765d",
        "status": "",
        "dispName": "",
        "imageURL": "",
        "childCount": "",
        "childs": [
            {
                "name": "Sub1",
                "Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f98f",
                "childs": [
                    {
                        "name": "Template1",
                        "Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f981",
                        "status": "",
                        "dispName": "",
                        "imageURL": ""
                    },
                    {
                        "name": "Template2",
                        "Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f982",
                        "status": "",
                        "dispName": "",
                        "imageURL": ""
                    }
                ]
            },
            {
                "name": "Template3",
                "Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f981"

            }
        ]
    },
    {
        "name": "Root2",
        "Id": "ea0586e7-02cf-4359-94ba-8d9623590dfe",
        "childs": [
            {
                "name": "Sub2",
                "Id": "6f1b3a60-d295-413e-92ef-1c713446e6c9",
                "childs": [
                    {
                        "name": "Template4",
                        "Id": "6f1b3a60-d295-413e-92ef-1c713446e6c1"
                    },
                    {
                        "name": "Template5",
                        "Id": "6f1b3a60-d295-413e-92ef-1c713446e6c2"
                    }
                ]
            }
        ]
    }
];

sortNames(names);
Sign up to request clarification or add additional context in comments.

Comments

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.