Are we able to order by a boolean in prisma? like ...
# orm-help
b
Are we able to order by a boolean in prisma? like sort boolean
true
first and then
false
last
n
Hey 👋 , I just tried it out and yes you can order by boolean. For Example: Consider this post model.
Copy code
model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?
}
The following query would return records with true first and false last.
Copy code
const posts = await prisma.post.findMany({
    where: {},
    orderBy: {
      published: 'desc',
    },
  });
sort order
desc
will return records fields having field value as
true
, while order
asc
will return records having field value as
false
first. Here is a reference to orderBy documentation. Please let me know if you have any further queries.
✅ 1