How to resolve the algorithm Sort a list of object identifiers step by step in the Sather programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort a list of object identifiers step by step in the Sather programming language

Table of Contents

Problem Statement

Object identifiers (OID) are strings used to identify objects in network data.

Show how to sort a list of OIDs, in their natural sort order.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort a list of object identifiers step by step in the Sather programming language

Source code in the sather programming language

class MAIN is
   oid_lt (a, b: STR): BOOL is
      as ::= a.cursor.split('.');
      bs ::= b.cursor.split('.');
      loop
         na ::= #INT(as.elt!);
         nb ::= #INT(bs.elt!);
         if na /= nb then return na < nb; end;
      end;
      return as.size < bs.size;
   end;

   main is
      sorter: ARR_SORT_ALG{STR, ARRAY{STR}};
      input: ARRAY{STR} := |"1.3.6.1.4.1.11.2.17.19.3.4.0.10",
                            "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",
                            "1.3.6.1.4.1.11150.3.4.0.1",
                            "1.3.6.1.4.1.11.2.17.19.3.4.0.1",
                            "1.3.6.1.4.1.11150.3.4.0"|;
      sorted ::= input.copy;
      sorter.sort_by(sorted, bind(oid_lt(_, _)));

      #OUT+"unsorted:\n";
      loop #OUT+input.elt! + "\n"; end;

      #OUT+"sorted:\n";
      loop #OUT+sorted.elt! + "\n"; end;
   end;
end;

  

You may also check:How to resolve the algorithm Julia set step by step in the Phix programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the C++ programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the V programming language
You may also check:How to resolve the algorithm Faulhaber's formula step by step in the C programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Raku programming language