Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

could you please tell me how to sort array in ascending or descending order on button click .Actually I have a button on header "V" .using button click I want to display data in ascending order .Actually I am making a grid view in Ionic using angular js .But I want to sort that using button click .I need to sort table according to first column .Because user click on first column "V".

here is my code

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

Expected result after button click

Calls   Royal Dutch sell

p        a royal data

Xgtyu     test royal data

can we sort the column and display on view on button click ?

  <div class="row gray-20 mrginrightleft">
    <div class="col col-center " ng-repeat="d in data | filter:{checked: true}"><i class="button button-icon icon ion-arrow-down-b" ng-click="sortdata()"></i><strong>{{d.label}}</strong></div>
    <div class="col col-10 text-center ">
      <button class=" button-icon icon ion-gear-b" ng-click="openPopover($event)"></button>
    </div>
  </div>
  <div class="row mrginrightleft" ng-repeat="column in displayData | filter: query">
    <div class="col col-center brd" ng-repeat="field in column.columns" ng-show="data[$index].checked && data[$index].fieldNameOrPath===field.fieldNameOrPath">{{field.value}}</div>
    <div class="col col-10 text-center brd">
    </div>
  </div>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

EDIT: Sort both directions

Updated Plunker

To be able to sort in both directions, you should probably use a directive. Using a directive will allow you to create an isolated scope for each of the headings, this way they can keep track of their own current values.

.directive('sortHeader', function($timeout) {
  return {
    restrict: 'E',
    scope: {
      'label': '@',
      'sortstring': '&sortExp',
      'idx': '@index'
    },
    templateUrl: 'sort-header.html',
    link: function(scope, element, attrs) {
      scope.reverse = false;
      element.on('click', function(){
        $timeout(function(){
          scope.reverse = !scope.reverse;
        });
      });
    }
  };
});

This directive has properties for:

  1. label [string] The column header name.
  2. index [string] The column index in the original data set. Note, you cannot use the $index of the ng-repeat.
  3. sort-exp [method] This is a bindable method that you can use to retrieve the current index and reverse values to set your orderBy filter expression. The function passes two values: idx, reverse, in that order. These represent the index of the current element and reverse order as a boolean.

You use the directive as follows:

<sort-header label="{{d.label}}" index="{{d.index}}" sort-exp="setSort(idx, reverse)"></sort-header>

And, in your controller, you can bind to the sort-exp with a function:

$scope.setSort = function(idx, reverse){
  $scope.sortval = 'columns['+idx+'].value';
  $scope.reverse = reverse;
};

Finally, in your ng-repeat, you can set up your orderBy filter with the scope values that you used to define the sort expression (in this case $scope.sortval) and the sort order (in this case $scope.reverse):

ng-repeat="column in displayData | orderBy: sortval:reverse | filter: query"
share|improve this answer
    
awsome sir thanks a lot ..!!! – Pallavi Sharma Jun 3 at 20:46
    
Hi sir I have one Question ..can we toggle in between "ascending" and "descending" on same button click .in other words on button click we display in ascending order . Now I need to display on descending order on same button click – Pallavi Sharma Jun 3 at 21:20
    
I updated the Plunker and my answer. There was a major error in my previous approach, which was that I was relying on the $index value of the ng-repeat. This, of course, is flawed when you have hidden columns, because the index of the column in the repeat will not match that of the index of its corresponding object in your displayData. I fixed this by transforming the data object to add a fixed index property to each object. – jme11 Jun 4 at 8:32
    
thanks a lot sir...great job – Pallavi Sharma Jun 4 at 15:04
    
could you please see my last question stackoverflow.com/questions/30647801/… – Pallavi Sharma Jun 4 at 15:17

if your data is an array then you can use orderBy filter See official doc here : https://docs.angularjs.org/api/ng/filter/orderBy

share|improve this answer
    
yes I know this from this link but it I have nested array please check my plunker ..please check this displayData object – Pallavi Sharma Jun 3 at 16:18
    
actually i am display data by using two object data and displayData .That why I am facing from to sort my column – Pallavi Sharma Jun 3 at 16:22
    
@aorfeve do you have any idea .can another array and fill all collumn data – Pallavi Sharma Jun 3 at 16:24
    
please remove this answer if you don't know the answer because it is not correect answer – Pallavi Sharma Jun 3 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.