r1 - 21 Apr 2005 - 12:44:54 - CrawfordCurrieYou are here: TWiki >  Codev Web > SortNumerically
Tags:
, create new tag
Sort uses cmp when sorting formfields. So numerical formfields get sorted lexically, which is wrong.

Propose to sort using the following sort function instead:

my $number = qr/^[-+]?[0-9]+(\.[0-9]*)?([Ee][-+]?[0-9]+)?$/;

sub _compare {
    if( $_[0] =~ /$number/ && $_[1] =~ /$number/ ) {
        # when sorting numbers to largets first; this is just because
        # this is what date comparisons need.
        return $_[1] <=> $_[0];
    } else {
        return $_[0] cmp $_[1];
    }
}
that allows numerical orderings on number data, though it's a bit unpredictable when mixed with lexical data..

-- CrawfordCurrie - 21 Apr 2005

 
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback SourceForge.net Logo