0

I'm trying to enable table sorting. If I'm clicking on empid and ename I'm able to achieve sorting but when I'm clicking on Doj I'm not able to achieve sorting because of string type doj.

Somehow If I convert doj to date then I can achieve date wise sorting also.

How can I acheive datewise sorting?

For table sorting I have written the logic like this in the controller

$scope.sortColumn = "empId";
$scope.reverseSort = false;

$scope.orderByField = function(column){
    $scope.reverseSort = ($scope.sortColumn == column) ? !$scope.reverseSort : false;
    $scope.sortColumn = column;
}
$scope.sortByField = function(column){
    if($scope.sortColumn == column){
        return $scope.reverseSort ? 'arrow-down' : 'arrow-up'
    }
    return '';
}

<th ng-click="orderByField('doj')">Date Of joining<div ng-class="sortByField('doj')"></div></th>
   <tr ng-repeat="emp in employeeList | orderBy: sortColumn: reverseSort">

I'm iterating list of records. The list of records is like:

  • empid
  • ename
  • doj (date of joining , I'm getting string from the backend)
Jason Aller
  • 3,391
  • 28
  • 37
  • 36
  • Need to do a little bit more work to turn this into a [mcve] as well as fine tune the problem description a bit. Not clear why you can't parse dates and sort that way – charlietfl Jan 24 '18 at 04:20
  • [Already Answered on SO](https://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) – Ved Prakash Jan 24 '18 at 04:52
  • my mistake I need to parseit from string to date while table sorting. – Rishikesh Kumar Jan 24 '18 at 05:08
  • @VedPrakash I have written the generic logic in orderByField based on clicking on column header it sorting the record but when I'm clicking on doj Its not sorting . because I'm getting Doj as string from Backend side . I don't have much idea about angularjs . how can we acheive this – Rishikesh Kumar Jan 24 '18 at 05:17

1 Answers1

0

Use this Simple logic

 array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});
Liam
  • 22,818
  • 25
  • 93
  • 157
Ved Prakash
  • 1,390
  • 5
  • 22
  • 31
  • You posted only bit of question so not able to find out where i cab put so you need to call this logic where ever you want. – Ved Prakash Jan 24 '18 at 05:25
  • @VedPrakash- table sorting is working as expected but its not working when I'm clicking on doj column header. how do I get the corresponding column record so that I can convert from string to date . – Rishikesh Kumar Jan 24 '18 at 09:37