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

How sort string date format 24.01.2017 ( descending ) I tried with Date.parse('24.01.2017'); -> but incorrect format

How sort date like this? in controller or view Thanks

Getting data from api want sort by _title enter image description here

share|improve this question
    
add your html and javascript – Maximus 18 hours ago
    
Where a you while converting the date ? in a controller ? in the view ? You can usually sort dates in a ng-repeat with an orderby filter – Roux 18 hours ago
    
with orderBy : _title it sorting by first digit means 05.11.12 and 06.12.11 first will be with 05 – Андрей Гузюк 18 hours ago
up vote 1 down vote accepted

Try the orderBy filter with a custom comparing function:

JS:

$scope.entries = [
  {date: '05.02.2001'},
  {date: '01.20.1930'},
  {date: '03.20.2020'} 
]

$scope.compareDates = function(date1, date2) {
  console.log(date1)
  var split1 = date1.value.split('.');
  var split2 = date2.value.split('.');

  var date1compare = split1[2] + split1[1] + split1[0];
  var date2compare = split2[2] + split2[1] + split2[0];

  return (date1compare > date2compare ? 1 : -1)
}

HTML:

<div ng-repeat="entry in entries | orderBy:'date':false:compareDates">
    {{entry.date}}
</div>

Plunker: https://plnkr.co/edit/LETAFDoD5fub63tKI5Ne?p=preview

share|improve this answer

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.