0

I have got a JSON Array as shown below

[{
        "name": "Johnww",
        "city": "New York",
        "createDate": "2019-02-15T17:17:10.000+0530"
      },
      {
        "name": "SAAA",
        "city": "California",
        "createDate": "2001-02-22T16:24:17.000+0530"
      },
      {
        "name": "Vignesh",
        "city": "India",
        "createDate": "1984-03-02T12:48:55.000+0530"
      }]

I am using KnockOut JS to display data in Sorted Order on basis of createDate

I have tried as following

 var availablePeople = ko.observableArray(

      [{
        "name": "Johnww",
        "city": "New York",
        "createDate": "2019-02-15T17:17:10.000+0530"
      },
      {
        "name": "SAAA",
        "city": "California",
        "createDate": "2001-02-22T16:24:17.000+0530"
      },
      {
        "name": "Vigneshss",
        "city": "India",
        "createDate": "1984-03-02T12:48:55.000+0530"
      }]
   );

   availablePeople.sort(function (left, right) { return left.createDate == right.createDate ? 0 : (left.createDate < right.createDate ? -1 : 1) })

   ko.applyBindings(availablePeople);

http://jsfiddle.net/E7xPM/393/

But its displaying wrong results , could you please let me know how to fix this ? Thank you

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 1
    Please explain "wrong results". Seems to me the `availablePeople` are correctly sorted (oldest first). If you want them to be flipped, you can replace the `<` by a `>`. – user3297291 Jun 21 '18 at 14:10
  • thank you , but my concern is should i use any date related such as compare to ?? – Pawan Jun 21 '18 at 14:12
  • If your date strings are all in the same time zone and have the ISO pattern that is in your sample data, you can safely use a string sort. [This answer](https://stackoverflow.com/a/12192544/3297291) covers all the details. – user3297291 Jun 21 '18 at 14:16
  • can i use this way ?? availablePeople.sort(function (left, right) { return new Date(left.createDate) < new Date(right.createDate) }) – Pawan Jun 21 '18 at 14:20

0 Answers0