Search code examples
javascripttfsazure-devopstfs-workitemworkitem

How to sort the relations returned using get work items API?


I am writing a TFS extension in javascript where I am using the 'GetWorkItem' function within 'TFS/WorkItemTracking/RestClient' library.

 wiRestClient.getWorkItem(<workItemID>, null, null, Contracts.WorkItemExpand.All)
            .then(function success(workItem) {
                console.log(workItem); 
 });

the output generated by the code above is as below: Output

This PBI has about 40 tasks within it and they are fetched in random order by the API.

Is there a way that these relations are fetched in the order of their Id?

I process the relations returned in the result, fetch the Id from a forward relation, get the workItemId and add it to an array.

Now, this array has information about all the child workitems of the parent PBI. I tried to sort this array based on System.Id in the fields property. This is the function I use to sort the data:

 childWorkItems.sort(function(a,b) {
     return a["System.Id"] > b.["System.Id"]
 });
 console.log(childWorkItems);

This doesn't seem to work. The array is still in random order.


Solution

  • I solved by changing the sort function to

     childWorkItems.sort(function(a,b) {
          return a["System.Id"] - b.["System.Id"]
     });
     console.log(childWorkItems);