Sort filenames naturally with Qt

If you want to use QCollator to sort entries from the list of entries returned by QDir::entryList, you can sort the result with std::sort(): dir.setFilter(QDir::Files | QDir::NoSymLinks); dir.setSorting(QDir::NoSort); // will sort manually with std::sort auto entryList = dir.entryList(); QCollator collator; collator.setNumericMode(true); std::sort( entryList.begin(), entryList.end(), [&](const QString &file1, const QString &file2) { return collator.compare(file1, file2) < … Read more

Natural sort of alphanumerical strings in JavaScript

This is now possible in modern browsers using localeCompare. By passing the numeric: true option, it will smartly recognize numbers. You can do case-insensitive using sensitivity: ‘base’. Tested in Chrome, Firefox, and IE11. Here’s an example. It returns 1, meaning 10 goes after 2: ’10’.localeCompare(‘2’, undefined, {numeric: true, sensitivity: ‘base’}) For performance when sorting large … Read more

Sorting List in C#

This is called a “natural sort order”, and is usually employed to sort items like those you have, like filenames and such. Here’s a naive (in the sense that there are probably plenty of unicode-problems with it) implementation that seems to do the trick: You can copy the code below into LINQPad to execute it … Read more

PostgreSQL ORDER BY issue – natural sort

Since Postgres 9.6, it is possible to specify a collation which will sort columns with numbers naturally. https://www.postgresql.org/docs/10/collation.html — 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; Now just query as you … Read more

Natural sorting

Google: Python natural sorting. Result 1: The page you linked to. But don’t stop there! Result 2: Jeff Atwood’s blog that explains how to do it properly. Result 3: An answer I posted based on Jeff Atwood’s blog. Here’s the code from that answer: import re def natural_sort(l): convert = lambda text: int(text) if text.isdigit() … Read more