Sujet : Re: Numerically sorted arguments (in shell)
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.unix.shellDate : 16. Jun 2024, 05:56:39
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4lra6$3t7b2$2@dont-email.me>
References : 1 2 3
User-Agent : Pan/0.158 (Avdiivka; )
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']