Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Firestore Timestamp should have a comparator method #7711

Open
CodingDoug opened this issue Oct 19, 2023 · 3 comments
Open

FR: Firestore Timestamp should have a comparator method #7711

CodingDoug opened this issue Oct 19, 2023 · 3 comments

Comments

@CodingDoug
Copy link

CodingDoug commented Oct 19, 2023

Operating System

N/A

Browser Version

N/A

Firebase SDK Version

N/A

Firebase SDK Product:

Firestore

Describe your project's tooling

N/A

Describe the problem

The JS/TS API for Firestore's Timestamp (both web client Timestamp and node backend Timestamp) do not include a comparator function. This makes sorting accurately by Timestamp rather painful, as you have to write a function that manually compares both seconds and nanos. An easy workaround is to use toMillis, but it's lossy on nanos:

array.sort((a, b) => b.ts.toMillis() - a.ts.toMillis())

Instead, it would be great if there was a comparator method provided in the same way as the Firestore Timestamp.compareTo() provided by the Java SDK.

array.sort((a, b) => b.ts.compareTo(a.ts))

Steps and code to reproduce issue

N/A

Related issue for the nodejs SDK: googleapis/nodejs-firestore#1922

@CodingDoug CodingDoug added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Oct 19, 2023
@jbalidiong jbalidiong added api: firestore needs-attention and removed new A new issue that hasn't be categoirzed as question, bug or feature request labels Oct 19, 2023
@marcusx2
Copy link

Timestamp has the valueOf method for this purpose, though I think it's kind of weird that the timestamp needs to be stringified to be compared.

@CodingDoug
Copy link
Author

CodingDoug commented Oct 19, 2023

@marcusx2 I use valueOf to serialize a timestamp when JSON won't do (and it would be nice to have a deserializer function as well - I wrote one myself), but yeah, it's not exactly intuitive to use this for comparisons.

I do see here that comparison was the original intent of valueOf, but that's doing way more work than necessary for a nlogn sort: #2662

@ehsannas
Copy link
Contributor

Here is my understanding:

When you compare two values the comparison operators (e.g. <, >) first try to convert objects into comparable primitives. By implementing valueOf(), timestamp objects can be compared directly with each other using the comparison operators.

e.g.

const t1 = new Timestamp(...);
const t2 = new Timestamp(...);
if (t1 > t2) {
  console.log("t1 has a later time than t2");
} else {
  console.log("t2 has a later time than t1");
}

The reason that valueOf() returns a string, rather than a number, is that javascript's number type cannot unambiguously represent all possible values that Timestamp can take on.

It appears that Array.sort() does not use valueOf() but rather converts the objects into strings, and uses that string to compare the objects. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The FR is to implement a compareTo() method to help with cases such as sorting arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants