|< 1 2 >| | 18 Einträge, 2 Seiten |
@liste=($eins,$zwei,$drei);
@sortListe=($zwei,$eins,$drei);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use strict;
sub create_obj {
my $self = {};
bless $self;
return $self;
}
my $obj1 = create_obj();
my $obj2 = create_obj();
my $obj3 = create_obj();
$obj1->{DATE}="2005-01-01";
$obj2->{DATE}="2004-12-20";
$obj3->{DATE}="2005-01-02";
sub sort_rule {
my ($year1,$month1,$day1) = split /-/,$a->{DATE};
my ($year2,$month2,$day2) = split /-/,$b->{DATE};
return $year1 <=> $year2 or
$month1 <=> $month2 or
$day1 <=> $day2
;
}
my @list = ($obj1,$obj2,$obj3);
print $_->{DATE},"\n" for sort sort_rule @list;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use strict;
package Test;
sub new {
my $self = {};
bless $self;
return $self;
}
package main;
my $obj1 = Test->new();
my $obj2 = Test->new();
my $obj3 = Test->new();
$obj1->{DATE}="2005-01-01";
$obj2->{DATE}="2004-12-20";
$obj3->{DATE}="2005-01-02";
my @list = ($obj1,$obj2,$obj3);
print $_->{DATE},"\n" for sort {$a->{DATE} <=> $a->{DATE}} @list;
_ _ END _ _
2005-01-01
2004-12-20
2005-01-02
my @sort_liste = sort { $a->{DATE} <=> $b->{DATE} } @liste;
$a->{DATE} <=> $a->{DATE}
$a->{DATE} <=> $a->{DATE}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$eins->{DATE}="2005-01-01";
$zwei->{DATE}="2005-01-02";
$drei->{DATE}="2004-12-20";
$eins->{ID}=1;
$zwei->{ID}=2;
$drei->{ID}=5;
@liste=($eins,$zwei,$drei);
my @sort_liste = sort { $b->{DATE} <=> $a->{DATE} } @liste;
for ( @sort_liste ) { print "datum: ",$_->{DATE}, "\n";
print " id: ",$_->{DATE},"\n";}
datum: 2005-01-01
id: 1
datum: 2005-01-02
id: 2
datum: 2004-12-20
id: 5
das gleiche aber mit "cmp" ergibt
datum: 2005-01-02
id: 2
datum: 2005-01-01
id: 1
datum: 2004-12-20
id: 5
1
2
3
4
5
6
7
8
9
10
Binary "<=>" returns -1, 0, or 1 depending on whether the left argument
is numerically less than, equal to, or greater than the right argument.
If your platform supports NaNs (not-a-numbers) as numeric values, using
them with "<=>" returns undef. NaN is not "<", "==", ">", "<=" or ">="
anything (even NaN), so those 5 return false. NaN != NaN returns true,
as does NaN != anything else. If your platform doesn't support NaNs then
NaN is just a string with numeric value 0.
perl -le '$a = NaN; print "No NaN support here" if $a == $a'
perl -le '$a = NaN; print "NaN support here" if $a != $a'
|< 1 2 >| | 18 Einträge, 2 Seiten |