in reply to Re: Data Structure advice
in thread Data Structure advice

I think we both misread his question -- here is what I missed: Unique items are only line per line, and he wants to sort on one coulumn and then if a "tie" happens break it with second sort on another coulmn.

I am trying to think of a good way to do this.
Edited this should work as long as both are the same type:
@data = numeric_sorter(2,4, @data) sub numeric_sorter { my ($field_num, $field_num_secondary, @data) = @_; my @sorted_items = sort { if ($a->[$field_num] != $b->[$field_num]) { $a->[$field_num] <=> $b->[$field_num] } else { $a->[$field_num_secondary] <=> $b->[$field_num_secondary] }} @data; return @sorted_items; }


-Waswas

Replies are listed 'Best First'.
Re: Re: Re: Data Structure advice
by shemp (Deacon) on Nov 22, 2002 at 23:20 UTC
    using me above array of arrayrefs implementation, if you want to sort on column 3 and break ties with column 1, do this:
    @data = sort { $a->[3] <=> $b->[1] || $a->[1] <=> $b->[1] } @data;
    that can be extended indefinitely with or "||" clauses, but to completely generalize this is much harder.
Re: Re: Re: Data Structure advice
by Anonymous Monk on Nov 22, 2002 at 23:28 UTC
    actually the first column is a timestamp, and the third column is a hex number

      Is your only purpose to sort the data? Are you intending to write it back to a file sorted? What else are you intending to do with the data within your perl script?

      Half a dozens lines of the data would make things a lot easir to answer with authority.


      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.