in reply to How to place Search Results!!!
Sort the results array. The most fun way to do it is something like:
#!/usr/bin/perl -w use strict; use vars qw(@results); @results = ("Bean Burrito|0.69|5", "Seven-Layer Burrito|1.39|8", "Pintos -n- cheese|0.59|0", ); sub numerically_by_item { my($which)=@_; return sub { (split(/\|/,$a))[$which] <=> (split(/\|/,$b))[$which] } } sub alpha_by_item { my($which)=@_; return sub { (split(/\|/,$a))[$which] cmp (split(/\|/,$b))[$which] } } my $sortby; print "Sorted by price\n"; $sortby = numerically_by_item(1); print join("\n",sort $sortby @results); print "\n\n"; print "Sorted by name\n"; $sortby = alpha_by_item(0); print join("\n",sort $sortby @results); print "\n\n"; print "Sorted by messiness\n"; $sortby = numerically_by_item(2); print join("\n",sort $sortby @results); print "\n\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to place Search Results!!!
by ACJavascript (Acolyte) on Aug 15, 2003 at 20:01 UTC | |
by sgifford (Prior) on Aug 16, 2003 at 17:31 UTC | |
by ACJavascript (Acolyte) on Aug 19, 2003 at 01:45 UTC | |
by sgifford (Prior) on Aug 19, 2003 at 04:40 UTC |