1

I want to sort array by timestamp, but getting unsorted error

var userValue = {
       timestamp,
       username,
       token,
       userid,
       message                      
   };
userArray.push(userValue);

this is the array i am getting

enter image description here

userArray.sort(function(a, b) {
return a[0]['timestamp'] - b[0]['timestamp'];
});

i want sorted array on timestamp basis

5
  • Unless i'm missing something you have? Do you want ascending or descending? Commented Jul 19, 2019 at 11:11
  • 2
    can you add the array itself to your question? Commented Jul 19, 2019 at 11:13
  • i want descending order Commented Jul 19, 2019 at 11:13
  • 1
    Why are you doing userArray[childDatamsg.timestamp]? Your array has a length of 1.5 billion now. Use userArray.push(userValue) Commented Jul 19, 2019 at 11:20
  • i have updated the code using userArray.push(userValue) but getting 2 result?? Commented Jul 19, 2019 at 11:27

2 Answers 2

2

This code

userArray[childDatamsg.timestamp] = userValue;

adds an object at timestamp index. This is why you have an array with the length of 1563533788! Instead push the userValue object to the userArray

userArray.push(userValue);

Now, it will have indices from 0.

Then you can sort the array like this:

userArray.sort((a, b) => a.timestamp - b.timestamp)
Sign up to request clarification or add additional context in comments.

Comments

1

The following code sorts by the numerical value of timestamps. It ignores skips over array entries and performs string-to-number conversion of timestamps, if necessary. It assumes that besides 'string' and 'number', timstamps are of no other data type.

userArray.filter ( px_item => {
    return (px_item !== undefined);
})
.map ( px_item => {
    if (typeof px_item === "string") {
        return parseInt(px_item);
    } else {
        return px_item;
    }
})
.sort(function(a, b) {
    if (typeof a === "undefined") {
        return -1;
    } else {
        if (typeof b === "undefined") {
            return 1;
        } else {
            return Math.sign ( a['timestamp'] - b['timestamp'] );
        }
    }
});

The original code had the sort function wrong. This function is actually a comparison function to determine the relative order of two elements (which in the given use case would be array entries). The order is expressed as one of the numerical values -1 (a < b), 0 (a = b), and 1 ( a > b ) (in fact, for the result of the comparison to be processed correctly, it suffices that the result has the proper sign, so Math.sign could be eliminated).

3 Comments

@coder_B: It works with [,,,{ timestamp: "4"},,, {timestamp: 1}] from the console which is constructed as a trimmed-down analogue of your original data. If you can share your data, I'll be happy to check out the issue.
@coder_B for starters, in which way does it not work? Does it throw an error?
i made it work, array was not working outside the firebase code so i place the code inside the firebase code so it gave me correct result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.