in reply to Re^3: Sort mechanics problems ...
in thread Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)
Unfortunately perl sort is content to produce incorrect output in finite time. i.e. it chose to ignore the correct positioning of B and C in the list and produced CAB as output. The correct behaviour would be an infinite loop or a fatal error because the array in any order is an incorrectly returned result from function 'sort'.#!/usr/bin/perl use strict; use warnings; my $iteration = 0; print sort byStupid (qw(A B C)); sub byStupid { $iteration++; $iteration > 10000 and die "10000 is a lot to sort three things"; if ($b eq 'C' and $a eq 'A') { $b cmp $a; } else { $a cmp $b; } }
One world, one people
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Sort mechanics problems ... (efficiency)
by tye (Sage) on Jun 02, 2016 at 07:32 UTC | |
|
Re^5: Sort mechanics problems ...
by haukex (Archbishop) on Jun 02, 2016 at 08:29 UTC | |
by anonymized user 468275 (Curate) on Jun 02, 2016 at 10:51 UTC |