简体   繁体   English

AngularJS UI网格排序

[英]AngularJS UI-Grid Sort

How can i sort the following data response of ui-grid by date 如何按日期对ui-grid的以下数据响应进行排序

    this.$http.get(url)
        .success(data => {
            this.$scope.gridOptions.data = data //.slice(firstRow, firstRow + paginationOptions.pageSize);
        }).finally(() => { this.$scope.loading = false; this.$scope.loadAttempted = true; });

I get the reponse as an array of objects and one of the properties is of type DOB, which is the one i want to sort by. 我得到的响应是对象数组,其中一个属性是DOB类型,这是我要排序的属性。

here goes a sample of my array 这是我的阵列的样本

[
{
 "Name":"John",
 "DOB" : "12/07/1987"
}
{
 "Name":"Jack",
 "DOB" : "12/07/1989"
}
{
 "Name":"Sara",
 "DOB" : "12/07/1980"
}
]

Thanks, 谢谢,

You can use the Javascript sort method to get your desired result like this. 您可以使用Javascript排序方法来获得所需的结果,如下所示。

result = response.sort(function(a, b) {
    return new Date(a.DOB).getTime() - new Date(b.DOB).getTime()
})

The result should be something like this. 结果应该是这样的。

[
  { 
    "Name":"Sara",
    "DOB" : "12/07/1980"
  },
  {
    "Name":"John",
    "DOB" : "12/07/1987"
  },
  {
    "Name":"Jack",
    "DOB" : "12/07/1989"
  }
]

You can also read more about Javascript sort method on W3Schools. 您也可以在W3Schools上阅读有关Javascript排序方法的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM