5

I am new to angular js and have basic understanding of how filters work in angular js.I am stuck to sort my jSON data array as there are various parameters on which it has to get sorted.

My JSON array is in this format:

[
  {
     type:0,
     emp_name:"xyz",
     connected_on:"9876543210"
  },
  {
     type:1,
     emp_name:"",
     connected_on:"9876543210"
  },
  {
     type:1,
     emp_name:"abcd",
     connected_on:"9876543210"
  },
  {
     type:0,
     emp_name:"pqr",
     connected_on:"9876543210"
  }
]

Other combination of the same array can be:

[
  {
     type:0,
     emp_name:"",
     connected_on:"9876543210"
  },
  {
     type:1,
     emp_name:"xyz",
     connected_on:"9876543210"
  },
  {
     type:0,
     emp_name:"abcd",
     connected_on:"9876543210"
  }
]

Every array will have any one object where type:1 , will be there and after sorting it has to be the first element always.

After that,the sorted array should have all those elements whose emp_name:"" is there.

At last of the sorted array it should contain all the remaining elements sorted according to emp_name:"whatever names"

So the results should look something like this below:

[
  {
     type:1,
     emp_name:"xyz",
     connected_on:"9876543210"
  },
  {
     type:0,
     emp_name:"",
     connected_on:"9876543210"
  },
  {
     type:0,
     emp_name:"abcd",
     connected_on:"9876543210"
  }
]

Can anyone help me to write the filter to get the desired result.Thanks in advance.If any other info required please let me know.

Also connected_on is unique value so i am using track by JSONarr.connected_on

1
  • Lodash has a function for this: lodash.com/docs#orderBy if you would consider ordering the array on load Commented Jun 28, 2016 at 12:21

2 Answers 2

2

Sort function expect -1, 0, -1 as result. -1 means before, 0 means no change and 1 means later.

Now you have to sort based on 2 parameters, so its better to have an arithmetic value. Have higher value based on priority. So in following situation, we have to sort first based on type and then based on emp_name.

To sort ascending, return 1 for greater and -1 for smaller. For descending reverse the values. Now combining this for both keys you will get something like this:

var data=[{type:0,emp_name:"xyz",connected_on:"9876543210"},{type:1,emp_name:"",connected_on:"9876543210"},{type:1,emp_name:"abcd",connected_on:"9876543210"},{type:0,emp_name:"pqr",connected_on:"9876543210"}];

data.sort(function(a, b) {
  var _a = a.type > b.type ? -10 : a.type < b.type ? 10 : 0;
  var _b = a.emp_name > b.emp_name ? 1 : a.emp_name < b.emp_name ? -1 : 0;
  return _a + _b;
});
console.log(data)

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

2 Comments

thanks,it worked like charm,i missed the trick to take it -10
Glad, was able to help you. :-)
0

Suppose your array name emplist. You can search anything here. You will get the desired result. Use table for searching the data.

<input type="text" ng-model="search">
  <table>
    <th>type</th>
    <th>Emp name</th>
    <th>connected on</th>
    <tr ng-repeat="item in emplist|filter:search">
       <td>{{item.type}}<td>
       <td>{{item.emp_name}}<td>
       <td>{{item.connected_on}}<td>
    </tr>
 </table>

1 Comment

I dont have to search , i just need to order the elements while displaying using filter created in my controller.

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.