VibORM

Sorting

Order query results

Sorting

Order query results using orderBy.

Basic Sorting

// Ascending (default)
const users = await client.user.findMany({
  orderBy: { name: "asc" },
});

// Descending
const users = await client.user.findMany({
  orderBy: { createdAt: "desc" },
});

Multiple Fields

Sort by multiple fields:

// Array syntax
const users = await client.user.findMany({
  orderBy: [
    { lastName: "asc" },
    { firstName: "asc" },
  ],
});

// Sort by role then name
const users = await client.user.findMany({
  orderBy: [
    { role: "asc" },
    { name: "asc" },
  ],
});

Null Handling

Control where nulls appear:

// Nulls first
const users = await client.user.findMany({
  orderBy: {
    lastLogin: { sort: "desc", nulls: "first" },
  },
});

// Nulls last
const users = await client.user.findMany({
  orderBy: {
    lastLogin: { sort: "desc", nulls: "last" },
  },
});

Relation Sorting

Sort by related fields:

// Sort posts by author name
const posts = await client.post.findMany({
  orderBy: {
    author: { name: "asc" },
  },
});

// Sort by nested relation
const comments = await client.comment.findMany({
  orderBy: {
    post: {
      author: { name: "asc" },
    },
  },
});

Aggregate Sorting

Sort by relation count:

// Most posts first
const users = await client.user.findMany({
  orderBy: {
    posts: { _count: "desc" },
  },
});

// Most comments first
const posts = await client.post.findMany({
  orderBy: {
    comments: { _count: "desc" },
  },
});

Sorting Included Relations

Sort included relations:

const users = await client.user.findMany({
  include: {
    posts: {
      orderBy: { createdAt: "desc" },
      take: 5,
    },
  },
});

Examples

User List

async function getUserList(sortBy: "name" | "createdAt" | "email", order: "asc" | "desc") {
  return client.user.findMany({
    orderBy: { [sortBy]: order },
  });
}

Latest Posts

async function getLatestPosts(limit = 10) {
  return client.post.findMany({
    where: { published: true },
    orderBy: { publishedAt: "desc" },
    take: limit,
  });
}
async function getPopularAuthors() {
  return client.user.findMany({
    orderBy: {
      posts: { _count: "desc" },
    },
    include: {
      _count: { select: { posts: true } },
    },
    take: 10,
  });
}

Alphabetical with Null Handling

async function getProductsSorted() {
  return client.product.findMany({
    orderBy: [
      { category: { sort: "asc", nulls: "last" } },
      { name: "asc" },
    ],
  });
}

Complex Sorting

async function getLeaderboard() {
  return client.user.findMany({
    orderBy: [
      { points: "desc" },
      { gamesWon: "desc" },
      { createdAt: "asc" },  // Tiebreaker: oldest first
    ],
    take: 100,
  });
}

Sort Direction

ValueDescription
"asc"Ascending (A-Z, 0-9, oldest-newest)
"desc"Descending (Z-A, 9-0, newest-oldest)

Database Collation

String sorting depends on database collation:

// PostgreSQL: Case-sensitive by default
// MySQL: Depends on collation setting
// SQLite: Case-sensitive by default

// Use case-insensitive collation in PostgreSQL
const users = await client.user.findMany({
  orderBy: {
    name: "asc",  // Uses database collation
  },
});

On this page