in reply to Re: Complex conditional sort
in thread Complex conditional sort

Thank you everyone for your suggestions. I tried most of your examples, although a couple didn't work as I desired (unless I implemented them incorrectly). I have used Limbic~Region's second example, its tidy and functions well:
foreach my $col ( sort { my $aa = $products{$a}{Q} > 0 ? "A$a" : "B$a"; my $bb = $products{$b}{Q} > 0 ? "A$b" : "B$b"; ($aa <=> $bb || $aa cmp $bb) } keys %products ) {
Thanks again, Chris

Replies are listed 'Best First'.
Re^3: Complex conditional sort
by ikegami (Patriarch) on Mar 09, 2011 at 01:36 UTC

    Why did you reintroduce ($aa <=> $bb || $aa cmp $bb)? First, it goes against what you said ("each group must then be sorted in alphabetical order"). Secondly, it implies that you have colour names that start with numbers, and that doesn't make much sense. Maybe you are doing a cheap "natural sort", but you'd would at least need a no warnings; to accompany it.

    Update: Expanded to clarify.

      The code is part of a larger eCommerce CGI script. While its live, warnings are turned off, therefore it wasn't an issue even if its bad practice. The sort must be able to handle numbers alone, or letters alone, or a combination of both (which I didn't explain properly). That was just my natural attempt to do that. I haven't tried yet, but i'm assuming cmp alone might be able to handle it. Chris
        If by combination of both, you mean stuff like "abc5def", it's currently being sorted to come after "abc10def".