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

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’. It was tested in Chrome, Firefox, and Internet¬†Explorer¬†11. Here’s an example. It returns 1, meaning 10 goes after 2: ’10’.localeCompare(‘2’, undefined, {numeric: true, sensitivity: ‘base’}) For performance when … 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

How to perform natural (lexicographic) sorting in R? [duplicate]

I don’t think “alphanumeric sort” means what you think it means. In any case, looks like you want mixedsort, part of gtools. > install.packages(‘gtools’) […] > require(‘gtools’) Loading required package: gtools > n [1] “abc21” “abc2” “abc1” “abc01” “abc4” “abc201” “1b” “1a” > mixedsort(n) [1] “1a” “1b” “abc1” “abc01” “abc2” “abc4” “abc21” “abc201”