sorting objects with "sort" and subroutine

From: Jay Eckles (j.eckles_at_computer.org)
Date: 11/27/04


Date: 27 Nov 2004 10:55:56 -0800

I'm having some difficulty figuring out why a bit of code isn't
working. I'm hoping you can help.

I have a funciton that retuns an array of objects. I have defined the
class myself (the definition is below, it's quite short). I want to
sort that array, using a subroutine to define how the objects should
be sorted. Here's the line I use to call sort:

@events = sort calendar::timeCompare @events

Where calendar::timeCompare is the subroutine I've defined in a
package I called calendar.

The problem is that in timeCompare, I'm not able to access any of the
members of the objects $a and $b. For example,

package calendar ;
...
sub timeCompare{
   print STDERR "a when: $a->{'when'}\n" ;
   print STDERR "a summary: $a->{'summary'}\n" ;
...

gives me the following output:

a when:
a summary:

(I know that the object have values for 'when' and 'summary' because I
use the objects, after the sort call, and they have values).

Without access to these members, I can't sort the objects. Is there
any trick to sorting objects as opposed to scalar values?

Thanks for any assistance you can lend.

Jay Eckles

PS - here's the class definition:

###Event class
package Event ;

sub new {
   my($class) = shift;

   bless {
      "date" => undef,
      "when" => undef,
      "location" => undef,
      "summary" => undef,
      "details" => undef
   }, $class;
}

PPS - this is part of an open source project called CGI Calendar,
http://sourceforge.net/projects/cgicalendar



Relevant Pages

  • Re: sorting objects with "sort" and subroutine
    ... Jay Eckles wrote on 27 Ноябрь 2004 18:55: ... > I have a funciton that retuns an array of objects. ... Here's the line I use to call sort: ... > package I called calendar. ...
    (comp.lang.perl.misc)
  • Re: Port Map Array
    ... Here is the array definition within my main module architecture. ... Instead of repeating the sort routine multiple times within my main ... the array to another entity or package to do the work, ... two signals in the sorter entity, one enable and one sort_now signal. ...
    (comp.lang.vhdl)
  • Port Map Array
    ... I am attempting to take a 2 dimentional array, and port map it to a ... Instead of repeating the sort routine multiple times within my main ... I would like to pass on (port map) ... the array to another entity or package to do the work, ...
    (comp.lang.vhdl)
  • Re: "Sorting" assignment
    ... And many others prefer to call partition exchange because "quicksort" ... bin B depending on whether it is greater than, ... If the array is already sorted, this means that you end up ... attempt to sort them. ...
    (comp.programming)
  • Re: A Fast sorting algorithm for almost sorted data
    ... far my compressor has potential but is nowhere near ready. ... It does however make heavy use of sorting. ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ...
    (comp.compression)