this post was submitted on 24 Apr 2025
204 points (97.2% liked)

Comic Strips

17771 readers
561 users here now

Comic Strips is a community for those who love comic stories.

The rules are simple:

Web of links

founded 2 years ago
MODERATORS
all 50 comments
sorted by: hot top controversial new old
[–] jordanlund@lemmy.world 28 points 2 months ago (2 children)

It's sorting correctly.

11 comes after 1.

The problem is the data needs to be fixed. 01, 02, 03...

[–] logicbomb@lemmy.world 11 points 2 months ago (1 children)

It's not sorting correctly. Right above the listing it specifically says "(sorting by #)".

If it says it is sorting by number, but then it sorts alphabetically, then it isn't sorting correctly.

[–] jordanlund@lemmy.world 7 points 2 months ago (1 children)

Problem is it can't sort by number of there are text values in file name. "11" is a number. "11." is not. :) Fun with computers!

[–] logicbomb@lemmy.world 10 points 2 months ago (1 children)

Both "11" and "11." are strings, because the context is listing filenames. Filenames are not numbers. They are strings. If you sort filenames by number, you are asking the computer to interpret the string as having a number inside it. At worst, it might interpret "11" as an integer and "11." as a floating point number, because that syntax is often used to specify a floating point number in programming. But even then, it could still sort them correctly.

I don't mean to start an argument, but as a professional programmer, there are just some things that I know.

[–] fushuan@lemm.ee 5 points 2 months ago (2 children)

Are you intentionally ignoring that the actual names of the files are "11. EpisodeEleven.mp3"? There's whitespaces and a bunch of letters there.

I'm also a professional programmer, and assuming that sorting by numbers code would try to grab the first block of strings until the first whitespace is a big assumption I would not make. I'd say that after trying to convert everything but the extension to a number for sorting it failed so it defaulted to string sorting for everything else.

load more comments (2 replies)
[–] rumschlumpel@feddit.org 27 points 2 months ago

That's just alphabetical sorting. There's other sorting styles that would put 11 after 2, but those aren't available everywhere.

[–] tal@lemmy.today 22 points 2 months ago* (last edited 2 months ago) (3 children)

Assuming that the leading number and period is part of the filename:

  1. If the directory-browsing thing supports natural sorting of numbers, you can use that; this will detect numbers within the filename and sort by them. For example, I use emacs's dired to browse directories and play movies. It can modify the flags passed to ls, and ls supports natural sorting of numbers in filenames with the -v flag. Doing this in dired means C-u s v RET, and the directory will be displayed sorted numerically.

  2. If the directory-browsing thing doesn't support that, rename to a "0"-padded format such that a lexicographic order has the same ordering as numeric order:

     $ for f in *; do
         n="$(printf %04d ${f%%.*})"
         mv "$f" "$(echo $f|sed s/^[0-9]*/$n/)"
     done 
    

    That way,

     1. Episode_One.mp4
    

    will become

     0001. Episode_One.mp4
    

    and so forth, and so software that can only do lexicographic orderings is happy.

If the leading number and period isn't part of the filename, then we need to parse human-language strings.

$ npm install words-to-numbers

$ for f in *; do
    w="$(echo $f|sed -r "s/^Episode_([^.]*).mp4/\1/")"
    n=$(echo $w|node -e 'wn=require("words-to-numbers"); const l=require("readline").createInterface({input:process.stdin, output:process.stdout, terminal:false}); rl.on("line", l=> {console.log(wn.wordsToNumbers(l));})')
    nf=$(printf %04d $n)
    mv "$f" "$nf. $f"
done

That'll rename to the format mentioned above in option 2:

Episode_One.mp4

will become

0001. Episode_One.mp4

And lexicographic sorting will work.

[–] hemko@lemmy.dbzer0.com 24 points 2 months ago

This is why I love Lemmy. There's a meme post and then someone writes a 7 page long technical solution like they were paid contributor at stack overflow.

Never change <3

[–] Buffalox@lemmy.world 6 points 2 months ago (1 children)

Wow is it really that simple? I wonder why not everybody does that! 🤣

[–] tal@lemmy.today 5 points 2 months ago* (last edited 2 months ago) (1 children)

The first case is the most-likely for most people, and the simplest to do. Most directory browsers do support numeric sorting.

In the second case, I provided a Perl program in another comment in the thread that provides a generic way to do this with one command for nearly all files of this sort.

The third case, where human-language stuff needs to be parsed, true enough, doesn't just have a button to push.

[–] rumschlumpel@feddit.org 1 points 2 months ago* (last edited 2 months ago) (1 children)

The first case is the most-likely for most people, and the simplest to do. Most directory browsers do support numeric sorting.

That may be true for terminal applications, but even on Linux most GUI file browsers don't support that, at least not out of the box.

[–] tal@lemmy.today 2 points 2 months ago* (last edited 2 months ago)

It looks like all of the Windows File Explorer, MacOS Finder, GNOME Files (nee Nautilus), and KDE Dolphin can. I suspect that that covers the overwhelming majority of users.

[–] ICastFist@programming.dev 4 points 2 months ago

You know what, I'll just manually rename the files

[–] pageflight@lemmy.world 11 points 2 months ago

So true. I recently learned there's a Python package for natural sorting, similar in other languages.

[–] Skullgrid@lemmy.world 8 points 2 months ago (1 children)
[–] SculptusPoe@lemmy.world 5 points 2 months ago (1 children)

You are correct, she should have used leading '0's. Really, they could have fixed that sort. Excel does it without leading '0's. Excel also does a lot of other nonsense because it is trying too hard, maybe I prefer the dumb sort. I try to get my people to start dated files with YYYYMMDDHHMM just because the file sorting is sort of dumb, but they insist on MMDDYYYY so all the years get jumbled together by month...

[–] knightly@pawb.social 7 points 2 months ago* (last edited 2 months ago)

This is 100% my pet peeve. Obviously Y-M-D-H-M is the correct format for timestamps because an alphanumeric sort will put them in chronological order.

[–] ElectroLisa@lemmy.blahaj.zone 5 points 2 months ago

Someone's got wrong locale set

[–] zarkanian@sh.itjust.works 5 points 2 months ago

That's why you give them names starting with 001, 002, etc.

[–] Nougat@fedia.io 1 points 2 months ago

Sort by a field that isn't the file name.