Deutsch   English   Français   Italiano  
<v4lra6$3t7b2$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.unix.shell
Subject: Re: Numerically sorted arguments (in shell)
Date: Sun, 16 Jun 2024 04:56:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <v4lra6$3t7b2$2@dont-email.me>
References: <v4grk8$2pp51$1@dont-email.me> <v4l5p1$3lqic$10@dont-email.me>
	<v4ll54$3sd11$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 16 Jun 2024 06:56:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="226f25248701f9e2b898433032a62efe";
	logging-data="4103522"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18F2DZecv8TYLmWOosmBzNw"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:/r/BxQH7YukmqqmhBkAK5j+tq64=
Bytes: 2064

On Sun, 16 Jun 2024 05:11:29 +0200, Janis Papanagnou wrote:

> I don't know of this feature in Perl or Python; please provide some hint
> if there is a feature like the one I need. Some code samples for
> demonstration of your point are also welcome.

Python solution:

    import re

    items = \
        [
            "P1.HTM", "P10.HTM", "P11.HTM", "P2.HTM", "P3.HTM",
            "P4.HTM", "P5.HTM", "P6.HTM", "P7.HTM", "P8.HTM", "P9.HTM",
        ]

    print(items)

    print \
      (
        sorted
          (
            items,
            key = lambda f :
                tuple
                  (
                    (lambda : p, lambda : int(p))[i % 2 != 0]()
                        for i, p in enumerate(re.split("([0-9]+)", f))
                  )
          )
      )

output:

    ['P1.HTM', 'P10.HTM', 'P11.HTM', 'P2.HTM', 'P3.HTM', 'P4.HTM', 'P5.HTM', 'P6.HTM', 'P7.HTM', 'P8.HTM', 'P9.HTM']
    ['P1.HTM', 'P2.HTM', 'P3.HTM', 'P4.HTM', 'P5.HTM', 'P6.HTM', 'P7.HTM', 'P8.HTM', 'P9.HTM', 'P10.HTM', 'P11.HTM']