Skip to content

Instantly share code, notes, and snippets.

@jamesholcomb
Created May 20, 2023 16:34
Show Gist options
  • Save jamesholcomb/4288278d57965f787ea0dcd077abc134 to your computer and use it in GitHub Desktop.
Save jamesholcomb/4288278d57965f787ea0dcd077abc134 to your computer and use it in GitHub Desktop.
Sort mongodb array of ObjectId
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