Sort a list of object identifiers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (prevent code dumped in as the task description from being interpreted as wiki markup)
(→‎{{header|Perl 6}}: Added Perl 6 solution)
Line 198: Line 198:


</lang>
</lang>

=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>use v6;

.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.2
.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
.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>
{{out}}
<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.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.3
.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.10
.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>

Revision as of 02:52, 8 July 2016

Sort a list of object identifiers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Sort by OID numbers.

OID - Object Identifiers are used in Network data These numbers can vary in length, and have a dot "." between them. The individual number can be of different number of digits also.

Example:

======================================
INPUT

@arrayOID = 
(
  ".1.3.6.1.4.1.11.2.17.19.3.4.0.10",
  ".1.3.6.1.4.1.11150.3.4.0.2",
  ".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",
  ".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"
);

========================================
OUTPUT - Correctly Sorted

# 01 | .1.3.6.1.4.1.11.2.17.19.2.0.9 |
# 02 | .1.3.6.1.4.1.11.2.17.19.2.0.79 |
# 03 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
# 04 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 |
# 05 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 |
# 06 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 |
# 07 | .1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
# 08 | .1.3.6.1.4.1.11.2.17.19.3.4.0.19 |
# 09 | .1.3.6.1.4.1.11.2.17.19.3.4.0.22 |
# 10 | .1.3.6.1.4.1.11.2.17.19.3.4.0.25 |
# 11 | .1.3.6.1.4.1.11.2.17.19.3.4.0.31 |
# 12 | .1.3.6.1.4.1.11.2.17.19.3.4.0.32 |
# 13 | .1.3.6.1.4.1.11.2.17.3773.0.1 |
# 14 | .1.3.6.1.4.1.11.2.17.3773.0.2 |
# 15 | .1.3.6.1.4.1.11150.3.4.0.1 |
# 16 | .1.3.6.1.4.1.11150.3.4.0.2 |
# 17 | .1.3.6.1.4.1.11150.3.4.0.11 |
# 18 | .1.3.6.1.4.1.11150.3.4.0.21 |


J

Data:

<lang J>oids=:<@-.&' ';._2]0 :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.2
 .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
 .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>

In other words, for each line in that script, remove the spaces and put the rest in a box.

Sorting:

<lang J> >(/: __&".;._1&.>) oids .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.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.3 .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.10 .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 </lang>

In other words, for our sort key, we break the contents of each box by the initial character and treat the remainder as numbers.

We also pull the result out of its boxes for display purposes.

Perl

<lang perl>

  1. !/usr/bin/perl
      1. --------------------------
      2. Sort: -OID - Numeric
      3. Bert Mariani 2016-07-07
      4. --------------------------

@arrayOID = () ; @arrayOIDSorted = () ;

@arrayOID = (

 ".1.3.6.1.4.1.11.2.17.19.3.4.0.10",
 ".1.3.6.1.4.1.11150.3.4.0.2",
 ".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",
 ".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"

);

### One big long line ... made readable
@arrayOIDSorted =                                                       
        map { $_->[0] }                                                     
              sort { $a->[1] cmp $b->[1] }                                    
                     map { [ $_,                                                  
                             join , map { sprintf("%8d",$_) } 
                             split( /\./, $_) 
                           ] 
                         } 
                     @arrayOID;      


   print "\nAfter OID Sort \n\n";
   $ptrN  = 0;   
   $entry = 1;
   while ( $arrayOIDSorted[$ptrN] )
   { 
      print "$entry | $arrayOIDSorted[$ptrN] |\n" ;
      $entry++;
      $ptrN++;
   }
   print "\nEnd OID Sort \n\n";
                          
      1. =========================================
  1. After OID Sort
  2. 1 | .1.3.6.1.4.1.11.2.17.19.2.0.9 |
  3. 2 | .1.3.6.1.4.1.11.2.17.19.2.0.79 |
  4. 3 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
  5. 4 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 |
  6. 5 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 |
  7. 6 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 |
  8. 7 | .1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
  9. 8 | .1.3.6.1.4.1.11.2.17.19.3.4.0.19 |
  10. 9 | .1.3.6.1.4.1.11.2.17.19.3.4.0.22 |
  11. 10 | .1.3.6.1.4.1.11.2.17.19.3.4.0.25 |
  12. 11 | .1.3.6.1.4.1.11.2.17.19.3.4.0.31 |
  13. 12 | .1.3.6.1.4.1.11.2.17.19.3.4.0.32 |
  14. 13 | .1.3.6.1.4.1.11.2.17.3773.0.1 |
  15. 14 | .1.3.6.1.4.1.11.2.17.3773.0.2 |
  16. 15 | .1.3.6.1.4.1.11150.3.4.0.1 |
  17. 16 | .1.3.6.1.4.1.11150.3.4.0.2 |
  18. 17 | .1.3.6.1.4.1.11150.3.4.0.11 |
  19. 18 | .1.3.6.1.4.1.11150.3.4.0.21 |
  20. End OID Sort
  21. ===========================================

</lang>

Perl 6

Translation of: Perl

<lang perl6>use v6;

.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.2
   .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
   .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>

Output:
.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.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.3
.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.10
.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