Created
May 20, 2023 16:34
-
-
Save jamesholcomb/4288278d57965f787ea0dcd077abc134 to your computer and use it in GitHub Desktop.
Sort mongodb array of ObjectId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Types } from "mongoose" | |
interface X { | |
_id: Types.ObjectId | |
} | |
const id1 = new Types.ObjectId() | |
const id2 = new Types.ObjectId() | |
const id3 = new Types.ObjectId() | |
const foo: X[] = [{ _id: id2},{_id: id1},{_id: id3}] | |
const bar = [...foo].sort((a,b) => a._id.toString().localeCompare(b._id.toString())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment