Sort a list of object identifiers: Difference between revisions

Content added Content deleted
(→‎Task: add line in test-case that i accidentally left out)
(→‎{{header|Perl 6}}: update to comply with new task description)
Line 477: Line 477:
=={{header|Perl 6}}==
=={{header|Perl 6}}==


The <tt>sort</tt> routine accepts a transform function at the first argument, which we use here to sort each string as the list of integers that it represents. (Lists of numbers are automatically sorted lexicographically with numeric comparison.)
{{update|Perl 6|the format description and test-case in the task description have been updated}}


<lang perl6>.say for sort *.split('.')».Int, <
{{trans|Perl}}
1.3.6.1.4.1.11.2.17.19.3.4.0.10
<lang perl6>use v6;
1.3.6.1.4.1.11.2.17.5.2.0.79

1.3.6.1.4.1.11.2.17.19.3.4.0.4
.say for sort { $^oid.comb(/\d+/).fmt('%08d') }, <
.1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11150.3.4.0.1
.1.3.6.1.4.1.11150.3.4.0.2
1.3.6.1.4.1.11.2.17.19.3.4.0.1
.1.3.6.1.4.1.11.2.17.19.3.4.0.19
1.3.6.1.4.1.11150.3.4.0
.1.3.6.1.4.1.11150.3.4.0.1
.1.3.6.1.4.1.11.2.17.19.3.4.0.22
.1.3.6.1.4.1.11.2.17.19.3.4.0.2
.1.3.6.1.4.1.11150.3.4.0.11
.1.3.6.1.4.1.11.2.17.19.3.4.0.1
.1.3.6.1.4.1.11.2.17.3773.0.2
.1.3.6.1.4.1.11.2.17.19.2.0.79
.1.3.6.1.4.1.11150.3.4.0.21
.1.3.6.1.4.1.11.2.17.19.2.0.9
.1.3.6.1.4.1.11.2.17.19.3.4.0.25
.1.3.6.1.4.1.11.2.17.19.3.4.0.32
.1.3.6.1.4.1.11.2.17.19.3.4.0.4
.1.3.6.1.4.1.11.2.17.19.3.4.0.31
.1.3.6.1.4.1.11.2.17.19.3.4.0.3
.1.3.6.1.4.1.11.2.17.3773.0.1
>;</lang>
>;</lang>


{{out}}
{{out}}
<pre>
<pre>.1.3.6.1.4.1.11.2.17.19.2.0.9
.1.3.6.1.4.1.11.2.17.19.2.0.79
1.3.6.1.4.1.11.2.17.5.2.0.79
.1.3.6.1.4.1.11.2.17.19.3.4.0.1
1.3.6.1.4.1.11.2.17.19.3.4.0.1
.1.3.6.1.4.1.11.2.17.19.3.4.0.2
1.3.6.1.4.1.11.2.17.19.3.4.0.4
.1.3.6.1.4.1.11.2.17.19.3.4.0.3
1.3.6.1.4.1.11.2.17.19.3.4.0.10
.1.3.6.1.4.1.11.2.17.19.3.4.0.4
1.3.6.1.4.1.11150.3.4.0
.1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11150.3.4.0.1
</pre>
.1.3.6.1.4.1.11.2.17.19.3.4.0.19
.1.3.6.1.4.1.11.2.17.19.3.4.0.22
.1.3.6.1.4.1.11.2.17.19.3.4.0.25
.1.3.6.1.4.1.11.2.17.19.3.4.0.31
.1.3.6.1.4.1.11.2.17.19.3.4.0.32
.1.3.6.1.4.1.11.2.17.3773.0.1
.1.3.6.1.4.1.11.2.17.3773.0.2
.1.3.6.1.4.1.11150.3.4.0.1
.1.3.6.1.4.1.11150.3.4.0.2
.1.3.6.1.4.1.11150.3.4.0.11
.1.3.6.1.4.1.11150.3.4.0.21</pre>


Alternatively, using the approach used by the Perl solution, for comparison ''(input elided)'':
Alternatively, more golfed:


<lang Perl6>.say for sort *.split('.')».Int, <
<lang perl6>.say for sort *.comb(/\d+/).fmt('%08d'), <...>;</lang>
...
>;</lang>


Or if using a third-party module is acceptable ''(input elided)'':
Or if using a third-party module is acceptable:


<lang Perl6>use Sort::Naturally;
<lang Perl6>use Sort::Naturally;


.say for sort &naturally, <
.say for sort &naturally, <...>;</lang>
...
>;</lang>


=={{header|REXX}}==
=={{header|REXX}}==