firebase / firebase/firebase-js-sdk

FR: Firestore Timestamp should have a comparator method

Open
#7,711 3 comments 0 reactions 1 assignee Claimed by @MarkDuckworth View on GitHub
api: firestore feature request
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### 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](https://firebase.google.com/docs/reference/js/firestore_.timestamp) and node backend [Timestamp](https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/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()](https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.Timestamp#com_google_cloud_Timestamp_compareTo_com_google_cloud_Timestamp_) 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: https://github.com/googleapis/google-cloud-node/issues/7462

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.