Efficient sorting of SNMP oids

Peter Corlett abuse at cabal.org.uk
Sat Oct 31 19:51:58 GMT 2009

On 31 Oct 2009, at 16:03, B Maqueira wrote:
> I need to sort eficiently a large array (~9000) of SNMP OIDs.
> I am currently trying the following code:
> my @sorted_oids = map { $_->[0] }
>                                  sort { $a->[1] cmp $b->[1] }
>                                     map { [$_, pack('w*', split(/\./,
> $_))]  } @oids;
> But this fails since it outputs before
> than
> Any ideas?

That's because "w" format is a BER encoding, and this does not map  
integers into strings such that ordering is maintained. For example,  
16383 encodes to "\xff\x7f", but 16384 encodes to "\x81\x80\x00" which  
string-sorts earlier.

Relatively few encodings *do* maintain that ordering. Big-endian fixed- 
width does. So I'd use "N*" instead in your pack format, mainly  
because I'm not entirely sure whether the components of SNMP OIDs can  
ever be greater than 65535 and thus whether I could thus get away with  
"n*" to halve the storage required.

More information about the london.pm mailing list