Sort two dimensional array with multiple keys

From: Milkweed (chris.mielke_at_drake.edu)
Date: 10/30/03


Date: 29 Oct 2003 15:05:14 -0800

I am trying sort an multi-dimensional array with three elements in
each "row".

Here's a look at a small section of input data:

207.28.198.86 10.36.1.121 0310291642
207.28.198.86 10.36.1.121 0310291753
207.28.201.113 10.77.2.241 0310291642
207.28.200.113 10.75.2.87 0310291642
207.28.199.86 10.76.1.80 0310291642
207.28.198.104 10.1.3.153 0310291642
207.28.198.86 10.36.1.121 0310291324
207.28.199.104 10.2.2.77 0310291642
207.28.195.33 10.2.4.111 0310291642

These are timestamped NAT translations from a firewall.
What I want to do is sort this data by element 0 (global IP) as the
primary key and element 2 (timestamp) as the secondary key so I can
keep a historical record of NAT translations for a given global IP.

I can't seem to get the sort to work. Here is a copy of what I have
tried so far:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet::Cisco;
use Data::Sorting qw( :basics :arrays );

...

foreach (@xlate) { # @xlate is populated by a SNMP call to the
firewall
    next unless /^Global/;
    my ($global, $local) = (split /\s/)[1,3];
    push @entries, [$global,$local,$timestamp];
# each row of @entries now looks like the sample data above
}

#Sort entries by IP (primary key) and date (secondary key) and print
output
my @ordered = sorted_array( @entries,
    { engine=> 'packed', sortkey=>$_[0][0] },
    sortkey=>$_[0][2] );
#my $i = 0;
#keys my %h = @history;
#@h{ sort map pack('C4 A x N' => $->[0] =~
/(\d+)\.(\d+)\.(\d+)\.(\d+)/,
# $_->[2], $i++) => @history } = @history;
#my @ordered = @h{ sort keys %h };
open NEW, ">xlate-ordered"
    or die "Could not open xlate-ordered for writing: $!\n";
for my $row (@ordered) {
    print NEW "@$row\n";
}

As you can see I have tried using the "indexed sort" method from "A
Fresh Look at Efficient Perl Sorting" as well as the Data::Sorting
module with no success. Help with either option would be much
appreciated!

Thanks,
Chris



Relevant Pages

  • Re: Removing Sort Order?
    ... add an item between two existing items) if I were using AutoNum and letting ... seems sort of... ... whether you use an Autonumber primary key, ... Lookup Wizard, then modify it using ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Primary Key not sorted
    ... Are you willing to rely on any "natural" sort of records? ... > though the Company field is the Primary Key but if I ... When I Compact and Repair the database it ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Query doesnt always sort
    ... My table doesn't have a primary key. ... How do I impose a sort on the datasheet view? ... using a query or in Access imposing a sort order on the datasheet view. ... Access MVP 2002-2005, 2007 ...
    (microsoft.public.access.queries)
  • Re: Getting Access to count to 10
    ... Normally they are sorted to primary key order when you compact. ... > What you are talking about is the sort order when you look at the records. ... Therefore, to sort them in numeric order, all text strings ... > Sort cell will put the records in the correct order. ...
    (microsoft.public.access.gettingstarted)
  • Re: Custom Paging in asp.net Datgrid Using Sql
    ... Here is a solution to dynamically return a sort result set back. ... CREATE TABLE #FileDownLoadJobs ... RowID INT IDENTITY NOT NULL PRIMARY KEY, ... I have written A SP in SQL which will be used to Bind the grid. ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)