Home > database >  How to sort an array filled with Timestamps?
How to sort an array filled with Timestamps?

Time:11-08

I have a list where its elements are Timestamps in the form of

Timestamp(seconds=..., nanoseconds=...)

so I got

List myarr = [Timestamp(seconds=..., nanoseconds=...),Timestamp(seconds=..., nanoseconds=...),Timestamp(seconds=..., nanoseconds=...)]

How can I order this list? I have tried calling myarr.sort() but then I got the following error:

This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.

How can I sort the above mentioned array?

CodePudding user response:

What is Timestamp? Is it perhaps firebase Timestamp?

Either way, it has to implement Comparable if it should be used without defining the sort method manually.

Otherwise you'll have to do myArr.sort((a,b) => "do your own sort")

CodePudding user response:

For sort it in asc:

myarr.sort((a, b) => a.milliseconds - b.milliseconds);

For sort it desc

myarr.sort((a, b) => b.milliseconds - a.milliseconds);

CodePudding user response:

Those are good answers, thank you. However, for me the following worked:

..sort()
  • Related