question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow the classic natural sort on string columns

See original GitHub issue

Problem

There is often a need to do natural sort on string columns. Machines’ sort is not always sensible for humans. https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/

Prisma doesn’t support this currently in v2.7.1.

Suggested solution

Either configured at schema level for the desired column, or by passing a param to orderBy.

Alternatives

Discussed https://github.com/prisma/prisma/discussions/3693

Additional context

ASC (12 should be the last one!) image

DESC (6 should be the last one!) image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
meotimdihiacommented, Jun 24, 2022

Today, I find out this answer: https://stackoverflow.com/a/67975489/329424 If you use Postgresql then it can be sort natural by default.

– First create a collation with numeric sorting CREATE COLLATION numeric (provider = icu, locale = ‘en@colNumeric=yes’);

– Alter table to use the collation ALTER TABLE “employees” ALTER COLUMN “em_code” type TEXT COLLATE numeric;

0reactions
meotimdihiacommented, Jun 20, 2022

@janpio but $queryRaw doesn’t handle plat to nested data. This is what I am using for my website:

async function getChapters(
  prisma,
  chapter,
  checkVol2Chap1
): Promise<chapters[]> {
  let order = Prisma.sql`naturalsort("chap") DESC NULLS LAST, naturalsort("vol") DESC NULLS LAST, naturalsort("title") DESC NULLS LAST`

  if (checkVol2Chap1) {
    order = Prisma.sql`naturalsort("vol") DESC NULLS FIRST, naturalsort("chap") DESC NULLS LAST, naturalsort("title") DESC NULLS LAST`
  }

  const chapters = await prisma.$queryRaw<any>`
    SELECT a.id, a.hid, a.vol, a.title, a.chap, a.group_name, a.lang, chapters_groups.group_id FROM chapters a
    LEFT JOIN chapters_groups ON chapters_groups.chapter_id = a.id
    WHERE status IN ('ok') AND comic_id = ${chapter.comics.id} AND lang = ${chapter.lang}
    ORDER BY ${order} 
  `
  const r = []
  let i = 0
  for (const chapter of chapters) {
    if (chapter.id != r[i]?.id) {
      if (r[i]) {
        i++
      }
      r[i] = Object.assign(chapter, { chapters_groups: [] })
    }

    if (chapter.group_id) r[i].chapters_groups.push(chapter.group_id)
  }

  return r
}

I find libs that could convert plat to nest data but there is no good library.

Knex don’t do it too. I dunno how everyone use knex. There may be some things that I don’t know about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL ORDER BY issue - natural sort - Stack Overflow
-- First create a collation with numeric sorting CREATE COLLATION numeric (provider = icu, locale = 'en@colNumeric=yes'); -- Alter table to use the...
Read more >
How to Sort Mixed Numbers and Text Using String Functions
Learn how to sort alphanumeric data like employee numbers with complete control using Excel functions including FIND, LEN, MID, LEFT, RIGHT, and VALUE....
Read more >
Natural sort order for text - Microsoft Power BI Community
Is there a simple way to sort data in a natural order in PowerBI? i.e. being able to sort a text column to...
Read more >
Use numeric sorting rather than literal sorting - NI Community
There could also be a specialised string sort function which takes in a string array and provides an option for a numeric or...
Read more >
Nature sorting in UI for ASP.NET AJAX | Telerik Forums
Hi, I want to make a multiple sorting + natural sorting column. ... to maintain and to allow multi-column sorting with custom handling: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found