2

It works well in the X5 JSCore and node environment, but it doesn't work in the iOS WKWebView environment.Why?

Here is my code:

var test = [
    {name: "aaa",createTime: "2019-10-10T11:11:16.000+0000"},
    {name: "ccc",createTime: "2019-10-03T10:11:16.000+0000"},
    {name: "bbb",createTime: "2019-10-07T10:11:16.000+0000"},
    {name: "zzz",createTime: "2019-10-09T03:12:25.000+0000"},
]

const sortList = test.sort((a, b) => {
    return new Date(b.createTime) - new Date(a.createTime)
  })

  console.log(sortList);

I use this article as a reference, but it doesn't work in other contexts How to sort strings in JavaScript

If the ISO string were illegal, it would not work in any environment, but now it works in other environments.

Only iOS environment doesn't work.

why?

Can you help me?

  • iso is just a string. If you want to sort on it, you need to use the sort function with a lambda function to tell how to determine sort. Guillaume below has done what is generally needed. Depending on what you need to do, you may want to sort on `getTime()` or some other sub attribute of this. – Fallenreaper Oct 10 '19 at 15:14
  • 1
    These are not in a format Date can parse, in general. Specifically, the times for ccc, bbb, and zzz have a single digit in the date, while ISO format uses two digits (0-padded). That's in addition to how you can't subtract dates directly, you need to convert to timestamps such as with `getTime()` – IceMetalPunk Oct 10 '19 at 15:14
  • Define "doesn't work". – str Oct 10 '19 at 15:16
  • Now that you've added the zeros, just use string sorting: [How to sort strings in JavaScript](https://stackoverflow.com/q/51165/215552) – Heretic Monkey Oct 10 '19 at 15:17
  • @HereticMonkey I've referenced it, but it doesn't work in other environments – BaiClassmate Xiao Oct 10 '19 at 15:24
  • @str It doesn't sort, it doesn't report errors – BaiClassmate Xiao Oct 10 '19 at 15:27
  • @str iOS 13.1.2 WKWebView – BaiClassmate Xiao Oct 10 '19 at 15:29
  • Your date time strings are not valid in JavaScript. You missed the colon in the time zone expression. – str Oct 10 '19 at 15:29
  • @str This is when the server returns.How to modify locally? It doesn't work in iOS WKWebView – BaiClassmate Xiao Oct 10 '19 at 15:31
  • "If the ISO string were illegal, it would not work in any environment, but now it works in other environments." That is far from true. Every JavaScript implementation has their own rules of string parsing for dates. But only the [specified date time format](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) should be used. – str Oct 10 '19 at 16:11

2 Answers2

1

I think you should do subtract integers, like Unix timestamps, rather the two Date objects. It would be more bulletproof and less depending on the javascript environment it would run on.

eg:

const sortList = test.sort((a, b) => {
    return (new Date(b.createTime)).getTime() - (new Date(a.createTime)).getTime()
  })

Best

guillaumepotier
  • 6,893
  • 8
  • 40
  • 70
1

The date time strings you have are not valid in JavaScript as they are missing the colon within the time zone expression. You should try to change that on the backend first. If you can't you have to add the missing colon yourself.

let dateString = '2019-10-10T11:11:16.000+0000';
const colonPosition = 26;

if (dateString.length === 28) {
    dateString = dateString.substring(0, colonPosition) + ':' + dateString.substring(colonPosition);
}

console.log(dateString);

But note that just checking string length is a naive approach. Depending on what formats your backend returns, this could fail. Thus it would be best to fix it on the backend directly.

str
  • 33,096
  • 11
  • 88
  • 115