我想更改请求的JSON输出,现在它是在创建记录时排列的,
{"id":1,"title":"The Town","release_date":"2010-10-21","image":"/zX4fKmDXKGt4hlzhAJirdlRKFgO.jpg","user_id":null,"created_at":"2015-12-04T19:57:58.449Z","updated_at":"2015-12-04T19:57:58.449Z","movie_id":"23168","imdb_rating":"7.6"},
{"id":2,"title":"Interstellar","release_date":"2014-11-06","image":"/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg","user_id":null,"created_at":"2015-12-04T19:58:16.600Z","updated_at":"2015-12-04T19:58:16.600Z","movie_id":"157336","imdb_rating":"8.7"},
{"id":3,"title":"Django Unchained","release_date":"2013-01-16","image":"/5WJnxuw41sddupf8cwOxYftuvJG.jpg","user_id":null,"created_at":"2015-12-04T20:00:22.411Z","updated_at":"2015-12-04T20:00:22.411Z","movie_id":"68718","imdb_rating":"8.5"},
{"id":4,"title":"Moonrise Kingdom","release_date":"2012-05-30","image":"/uw88gWDC0W7AAEhMeQmtdXFV7yR.jpg","user_id":null,"created_at":"2015-12-04T20:00:41.054Z","updated_at":"2015-12-04T20:00:41.054Z","movie_id":"83666","imdb_rating":"7.8"},
{"id":5,"title":"Gone Girl","release_date":"2014-10-01","image":"/gdiLTof3rbPDAmPaCf4g6op46bj.jpg","user_id":null,"created_at":"2015-12-05T09:05:08.424Z","updated_at":"2015-12-05T09:05:08.424Z","movie_id":"210577","imdb_rating":"8.2"}
但我想按发布日期排列数据,所以它看起来像这样,
{"id":1,"title":"The Town","release_date":"2010-10-21","image":"/zX4fKmDXKGt4hlzhAJirdlRKFgO.jpg","user_id":null,"created_at":"2015-12-04T19:57:58.449Z","updated_at":"2015-12-04T19:57:58.449Z","movie_id":"23168","imdb_rating":"7.6"},
{"id":4,"title":"Moonrise Kingdom","release_date":"2012-05-30","image":"/uw88gWDC0W7AAEhMeQmtdXFV7yR.jpg","user_id":null,"created_at":"2015-12-04T20:00:41.054Z","updated_at":"2015-12-04T20:00:41.054Z","movie_id":"83666","imdb_rating":"7.8"},
{"id":3,"title":"Django Unchained","release_date":"2013-01-16","image":"/5WJnxuw41sddupf8cwOxYftuvJG.jpg","user_id":null,"created_at":"2015-12-04T20:00:22.411Z","updated_at":"2015-12-04T20:00:22.411Z","movie_id":"68718","imdb_rating":"8.5"},
{"id":5,"title":"Gone Girl","release_date":"2014-10-01","image":"/gdiLTof3rbPDAmPaCf4g6op46bj.jpg","user_id":null,"created_at":"2015-12-05T09:05:08.424Z","updated_at":"2015-12-05T09:05:08.424Z","movie_id":"210577","imdb_rating":"8.2"}
{"id":2,"title":"Interstellar","release_date":"2014-11-06","image":"/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg","user_id":null,"created_at":"2015-12-04T19:58:16.600Z","updated_at":"2015-12-04T19:58:16.600Z","movie_id":"157336","imdb_rating":"8.7"},
这可以使用Ruby on Rails吗?
*编辑*
我使用上面的JSON数据将它放入名为movies
的$ scope中。然后我使用ng-repeat输出数据。我不能在重复中使用orderBy
因为我将我的项目放在行中。但我认为如果我改变$scope.movies
中的顺序,那么使用javascript重新排列顺序也会有效。
答案 0 :(得分:1)
假设你正在使用json,你可以使用
json_array.sort_by { |json| Time.parse(json['release_date']) }
这里有2个注释:
Time.parse
在排序之前是一个开销(会更好)(但对于小型集合,这不是问题)release_date
不是日期而是时间......这有点尴尬。 ;)