9969

Angular orderBy with date string

Question:

I'm attempting to order events in my web app by date (in this case 'start' date from my db). I'm running into issues where the dates aren't sorted properly. I believe they are sorting alphanumerically rather than by date because my 'start' field is a string. My question is, how to best convert this field to Date for proper ordering? Relevant code below:

html:

<div ng-repeat="event in events | orderBy:'start'">{{event.city}}</div>

Angular Controller:

angular.module('MainCtrl', []).controller('MainController', function($scope, $routeParams, Events) { // pull empty event details from API based on routeParams to get all events $scope.id = ''; Events.get($scope.id).success(function(response) { $scope.events = response; }); });

Sample database object:

{ "_id": { "$oid": "54978d87ac1e3caaeee8452c" }, "event": "New Years Eve - Times Square", "city": "New York", "state": "New York", "date": "December 31, 2014", "start": "December 31, 2014", "end": "January 1, 2015", "radius": "400", "team_1": "", "team_2": "", "object_id": "10897094", "longitude": "-73.9848931", "latitude": "40.7591529", "photos": [] }

Answer1:

Turned out to be a very simple solution. I simply added the following code to my controller inside my Events.get:

for (var i = 0, max = response.length; i < max; i++) { $scope.events[i].start = new Date(response[i].start); }

Recommend

  • Generate a unique string based on a pair of strings
  • Receive list of elements in their visual order
  • Angular page doesn't refresh after data is added or removed
  • Not able to display correct data in table -AngularJS
  • Why doesn't a local variable live long enough for thread::scoped?
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Python ImageIO Gif Set Delay Between Frames
  • Floated image with variable width and heading with background image
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • Breaking out column by groups in Pandas
  • Textfile Structure (tables)
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Meteor helpers not available in Angular template
  • Change multiple background-images with jQuery
  • FileReader+canvas image loading problem
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Align navbar back button on right side
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Javascript Callbacks with Object constructor
  • Sending data from AppleScript to FileMaker records
  • How to get icons for entities from eclipse?
  • Proper folder structure for lots of source files
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • JaxB to read class hierarchy
  • Is there any way to bind data to data.frame by some index?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal