0

So i am getting data from my own server and i set the data to a variable called: items.

So whenever i do:

 if (this.items) {
                    this.items.sort(function (a, b) {
                        return +new Date(b.datum) - +new Date(a.datum);
                    });
                }

on windows and android it does sort my array but whenever i try this on a apple product like an iphone it doesn't sort on date so i seriously have no clue how?

8
  • Why there is a "+" before each new? Commented Jan 3, 2019 at 14:00
  • Do you get any error message in the debug-log? Did you insert a debug-logging-print into the sort-function to see if it's actually called? Then just print what each value (a and b) is and what the result of your calculation is. We can't help you, if you don't do simple debugging yourself :/ Commented Jan 3, 2019 at 14:34
  • @Jonathan the operation must be a type of 'any', 'number' or any enum type Commented Jan 3, 2019 at 15:13
  • @spyro well yes i did, but i do net get what it actually means because it generates me random numbers. but the weird thing is that why does it sort on windows and android but ios devices there is not 1 device that works so if you can tell me what you need so you can look at your self i can do that for you Commented Jan 3, 2019 at 15:15
  • Is b.datum a timestamp/number? Then you can and should just return b.datum - a.datum; or the other way around, depending on what you want to sort by. The "new Date" seems to make no sense to me, in addition to what @Jonathan said :/ Maybe the syntax is slightly wrong at the oment but some OS still accept it while other will not because of that slightly wrong syntax? Give it a try and report back. Commented Jan 3, 2019 at 15:20

4 Answers 4

2

I think it is the issue with the format of the date coming. IE and Safari supports a certain formats of Dates. Refer the link for details http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari/

For me the format coming was as follows :- "2015-11-07T15:04:46+0100" This format works fine in Chrome browser. But for iOS I had to remove the last 5 characters i.e "2015-11-07T15:04:46" for it to work.

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

Comments

1

The way sort works is by subtracting the value of two consecutive elements (a,b) of the array. if the result (a - b) is negative then it means that b is bigger than a, so the order is flipped and so on..

It means that you need to make sure that you are subtracting numbers. Here is an example :

this.items.sort(function (a, b) {
    const dateA = new Date(a.datum);
    const dateB = new Date(b.datum);
    return dateB.getTime() - dateA.getTime();
});

This will sort your array in a descending way. the getTime method returns a timestamp which is perfect to make calculations. However, if your datum already is a timestamp, than you dont need to create Date objects, you can subtracting them directly

1 Comment

Thankyou for the explanation. I get it. But it works 100% on windows and android as i am testing right now but every ios device is still not sorting. So i am getting data from my own server a json converted script i made my self to array and i push that array with a ngfor and it works and all. But i have tried like 5 different sort methodes and still every devices works but the only device it wont work is the iphone
0
let items = [
    { datum: 100, other: "..." },
    { datum: 105, other: "..." },
    { datum: 120, other: "..." },
    { datum: 130, other: "..." },
    { datum: 102, other: "..." }
];
console.log(JSON.stringify(items));
items.sort(function (a,b) {
    return a.datum - b.datum;
});

console.log(JSON.stringify(items));

This works just fine for me.

Comments

0

I had the same issue and I solved it by using the method below:

Replace the space to "T" in your date value before sorting

    this.messages = this.messages.map(m=>{
      let datum = m.datum.toString().replace(" ","T");
      m.datum = datum;
      return m;
    })

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.