Javascript sort json array

data =  [ 
   "{\"date\":\"08/09/2019\",\"time\":\"15:56:55\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"08/09/2019\",\"time\":\"15:56:59\",\"session\":0,\"part\":1,\"slide\":\"sd-004-03\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"08/09/2019\",\"time\":\"15:57:03\",\"session\":0,\"part\":\"4\",\"slide\":\"sd-005-01\",\"feedback\":\"\"}",
   "{\"date\":\"08/09/2019\",\"time\":\"15:57:03\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"08/09/2019\",\"time\":\"15:57:04\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":1}",
   "{\"date\":\"08/09/2019\",\"time\":\"15:57:05\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":2}",
   "{\"date\":\"07/09/2019\",\"time\":\"22:02:54\",\"session\":0,\"part\":\"1\",\"slide\":\"sd-002-01\",\"feedback\":\"\"}",
   "{\"date\":\"07/09/2019\",\"time\":\"22:02:54\",\"session\":0,\"part\":1,\"slide\":\"sd-002-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"22:02:56\",\"session\":0,\"part\":1,\"slide\":\"sd-002-02\",\"feedback\":\"\",\"page\":1}",
   "{\"date\":\"07/09/2019\",\"time\":\"22:02:57\",\"session\":0,\"part\":1,\"slide\":\"sd-002-02\",\"feedback\":\"\",\"page\":2}",
   "{\"date\":\"07/09/2019\",\"time\":\"22:02:59\",\"session\":0,\"part\":1,\"slide\":\"sd-002-02\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:35\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:35\",\"session\":0,\"part\":1,\"slide\":\"sd-004-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:37\",\"session\":0,\"part\":1,\"slide\":\"sd-004-03\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:40\",\"session\":0,\"part\":\"4\",\"slide\":\"sd-005-01\",\"feedback\":\"\"}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:40\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:42\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":1}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:56:43\",\"session\":0,\"part\":1,\"slide\":\"sd-005-01\",\"feedback\":\"\",\"page\":2}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:52:49\",\"session\":0,\"part\":\"1\",\"slide\":\"sd-002-01\",\"feedback\":\"\"}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:52:49\",\"session\":0,\"part\":1,\"slide\":\"sd-002-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:52:55\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:52:55\",\"session\":0,\"part\":1,\"slide\":\"sd-004-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"07/09/2019\",\"time\":\"21:52:58\",\"session\":0,\"part\":1,\"slide\":\"sd-004-03\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":1,\"slide\":\"sd-004-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":1,\"slide\":\"sd-004-01\",\"feedback\":\"\",\"page\":0}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":\"3\",\"slide\":\"sd-004-01\",\"feedback\":\"\"}",
   "{\"date\":\"04/09/2019\",\"time\":\"14:11:45\",\"session\":0,\"part\":1,\"slide\":\"sd-004-01\",\"feedback\":\"\",\"page\":0}"
]

sortedArray = sortArray(data)

console.log(sortedArray)

function sortArray(data) {
  var newarr = []

  // convert

  for (i=0;i<data.length;i++) {
    obj = JSON.parse(data[i])
    ts = new Date(obj.date + " " + obj.time).getTime()
    page = ""
    if (obj.page >= 0) page = obj.page

    newarr.push(
      {
        "ts": ts,
        "date": obj.date,
        "time": obj.time,
        "session": obj.session,
        "slide": obj.slide,
        "feedback": obj.feedback,
        "page": page,
        "raw": data[i]
      }
    )
  }

  // sort asc

  newarr.sort((function (a, b) { 
    return new Date(a.ts) - new Date(b.ts) 
  }))

  // construct to the original json / data

  var a = []

  for (i=0;i<newarr.length;i++) {
    a.push(newarr[i].raw)
  }

  return a
}

Live code: https://playcode.io/452749

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top