0

How can i sort the following data response of ui-grid by date

    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.

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,

1
  • So in your grid, you'll have 2 columns? Or just Name column? Commented Jan 21, 2016 at 10:54

1 Answer 1

1

You can use the Javascript sort method to get your desired result like this.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.