1

How can i get most recent or closest date from array of object , i tried to implement sortFunction but no luck, Any idea how can i acheive this task ?

ctrl.js

 $scope.data = [{
        fileDate: Wed Aug 10 2016 10: 10: 44 GMT - 0400(Eastern Daylight Time),
        filename: 'server.log'
    }, {
        fileDate: Tue Aug 30 2016 10: 48: 16 GMT - 0400(Eastern Daylight Time),
        filename: 'server1.log'
    }, {
        fileDate: Wed Aug 31 2016 12: 14: 46 GMT - 0400(Eastern Daylight Time),
        filename: 'server3.log'
    }]

function sortFunction(a, b) {
    var dateA = new Date(a.date).getTime();
    var dateB = new Date(b.date).getTime();
    return dateA > dateB ? 1 : -1;
};
$scope.data.sort(sortFunction);
console.log('DATE ARRAY', $scope.data);

main.html

<tr ng-repeat="file in data | orderBy:sortType:sortReverse">
                <td>{{file.filename}}<label ng-show="showText">currently recording</label></td>
                <td>{{file.fileDate  |date :  "dd.MM.y"}}</td>
                <td><button type="button" class="btn btn-primary" ng-click="downloadServerFile(file.filename)">download</button></td>
            </tr>
hussain
  • 4,747
  • 12
  • 51
  • 120
  • 2
    I hope this will help you:- http://stackoverflow.com/a/10124053/2437590 – Akshay Paghdar Aug 31 '16 at 18:40
  • 2
    If `fileDate` is the name of the property, what is `(a.date)` and `(b.date)` in your `sortFunction` ? – Mark C. Aug 31 '16 at 18:45
  • i am not sure how i will use this sort function in case of multiple objects – hussain Aug 31 '16 at 18:47
  • That is invalid JavaScript: the `fileDate` properties should probably be strings? Put them in quotes. Secondly, JavaScript does not recognise these as valid dates when doing `new Date(...)`: so the `sort` will have no effect. Use string values that are simple, like `2016-08-10 12:14:46`. – trincot Aug 31 '16 at 21:11
  • You should [fix your comparison function](http://stackoverflow.com/q/20883421/1048572). – Bergi Aug 31 '16 at 21:18

1 Answers1

1

Your JavaScript object is malformed: quotes are missing around the date strings.

Secondly, they represent strings that JavaScript cannot interpret as Dates.

Here is code to fix those strings before converting them to dates. Then reduce can be use to find the most recent date:

$scope.data = [{
    fileDate: 'Wed Aug 10 2016 10: 10: 44 GMT - 0400(Eastern Daylight Time)',
    filename: 'server.log'
}, {
    fileDate: 'Wed Aug 31 2016 12: 14: 46 GMT - 0400(Eastern Daylight Time)',
    filename: 'server3.log'
}, {
    fileDate: 'Tue Aug 30 2016 10: 48: 16 GMT - 0400(Eastern Daylight Time)',
    filename: 'server1.log'
}];

function dateFromString(str) {
    // Fix date string before conversion -- some spaces need to be removed:
    str = str.fileDate.replace(/([:-])\s+/g,'$1');
    return new Date(str);
}

function mostRecent(a, b) {
    return dateFromString(a) > dateFromString(b) ? a : b;
};

With the above functions you can pick the most recent object by doing:

var lastFile = $scope.data.reduce(mostRecent);

This answers your question. In comments you mentioned: "I just want to get most recent date value and make ng-show boolean value true".

For that you could add a new boolean property to each item, indicating whether it is the last one:

$scope.data.forEach(function (file) {
    file.mostRecent = file === lastFile;
});

You can then use that property in your ng-show attribute.

trincot
  • 211,288
  • 25
  • 175
  • 211
  • pop will remove the last item , I also want to display all list into ng-repeat, Any other solution ? – hussain Sep 01 '16 at 13:33
  • Yes, prefix the `pop()` with `slice(-1).` -- answer updated. Or else you sort in the opposite direction, and take `$scope.data[0]`. – trincot Sep 01 '16 at 13:43
  • what will happen i have sorting on date column in UI if user do sorting last item will become first in that case we will not have most recent date in `obj`, because my end goal is to add some string to recent date value on Ui side. I want to make `showText` variable true if `obj` and `$scope.data` dates match .Html added – hussain Sep 01 '16 at 14:09
  • In that case, don't sort the array at all, but find the most recent value without changing the array. Is that what you want? If so, I'll provide that as an answer. – trincot Sep 01 '16 at 14:27
  • Yes i just want to get most recent date value and make ng-show boolean value true – hussain Sep 01 '16 at 14:28
  • OK, that last part (about `ng-show`) was not really in your question. As a general rule, you should better ask this in a new question. But I have added in my answer the code to find the most recent element of the array, and how to add a boolean property to each element which indicates whether it is the most recent one. With that you should be able to get what you want. – trincot Sep 01 '16 at 14:43
  • Thanks alot for the help i got it working as expected. – hussain Sep 01 '16 at 14:57