Here is some (admittedly, very inmature) Haskell to solve this problem.
type Data = (Int, Int, String) bottom (b, _, _) = b top (_, t, _) = t ide (_, _, i) = i contains :: Data -> Data -> Bool x `contains` y = bottom x <= bottom y && top x >= top y rangeSort1 :: [Data] -> [(Data, [Data])] rangeSort1 r = map (\x -> (x, (filter (contains x) (filter (/=x) r)))) + r ---- test :: [Data] test = [ (11, 22, "A") , (22, 45, "B") , (22, 33, "C") , (25, 28, "D") , (47, 49, "E") ]
And here is the corresponding Perl, or at least, this is the closest I could get to this 'declarational' style in Perl:
sub bottom { shift->[0] }; sub top { shift->[1] }; sub ide { shift->[2] }; sub contains { bottom($_[0]) <= bottom($_[1]) && top($_[0]) >= top($_[ +1]) }; sub rangeSort1 { map { my $x = $_; [$x, [grep {contains($x, $_)} (grep { ide($_) ne ide($x) } @_) ] ] } @_ }; my $test = [ [11, 22, "A"] , [22, 45, "B"] , [22, 33, "C"] , [25, 28, "D"] , [47, 49, "E"] ];
If there's a Haskell/Perl guru out there with some free time, could you please improve on this?

Update: slightly improved Haskell:

rangeSort1 :: [Data] -> [(Data, [Data])] rangeSort1 r = mapMaybe (\x -> let l = filter (contains x) . filter (/ +=x) in case l r of [] -> Nothing otherwise -> Just (x, l r) ) r

In reply to Re: sorting and grouping by by rg0now
in thread sorting and grouping by by jperlq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.