Gerryjun has asked for the wisdom of the Perl Monks concerning the following question:

I?m trying to sort my hash of results from a query search.
By category (1)Exact $query, (2)All of the words in the $query,
and finally (3) any words in the $query.

I?m familiar with sorting alphabetically ex.

@RE_QUERY = sort { "$QUERY {$a}[3] $QUERY {$a}[2] $QUERY {$a}[0]" cmp + "$QUERY {$b}[3] $ QUERY {$b}[2] $QUERY {$b}[0]" } keys % QUERY;


But now I need to sort it depending on the presence of the $query in fields in the hash!

I may be using any of the two!
$titles{$PathFilename} = $title; $descriptions{$PathFilename} = $description;

or
$RESULTS{PathFilename}[0] = $title; $RESULTS{PathFilename}[1] = $description;

Haven?t decided what to use?

I need to sort it first by the $title or the presence in it then the $description using the three Categories for sorting.

Thank you for any help!

Replies are listed 'Best First'.
Re: Sorting a Hash by the presence of a $tring Argument
by MZSanford (Curate) on Oct 30, 2001 at 21:41 UTC
    More of a coding and Object design comment, but rather than maintaining multiple hashes, or arrays, why not use the HoH approach, like this :
    %RESULTS = ( pathFileA => { title => $title, desc => $descrip, }, pathFileB => { title => $title, desc => $descrip, }, );

    From there, the sorting is left as an excersise for the reader (always wanted to say that)
    i had a memory leak once, and it ruined my favorite shirt.
Re: Sorting a Hash by the presence of a $tring Argument
by pike (Monk) on Oct 31, 2001 at 01:18 UTC
    Not sure if I understood your problem correctly, but here's the solution to what I understood:

    @RE_QUERY = sort { for my $i (3, 2) { return $QUERY{$a}[$i] cmp $QUERY{$b}[$i] if defined $QUERY{$a}[$i] && defined $QUERY{$b}[$i] && $QUERY{$a}[$i] cmp $QUERY{$b}[$i]; } return $QUERY{$a}[0] cmp $QUERY{$b}[0]; } keys % QUERY;
    pike