Haskell:
import Data.List (groupBy,sort)
import Data.Char (toUpper)
p2b :: (Eq a,Ord a,Ord b)=>[(a,b)]->[(a,[b])]
p2b = let f :: [(a,b)] -> (a,[b])
f x = foldl (\(a,b) (_,y) -> (a,y:b)) (fst.head $ x,[snd.head $ x]) (tail x)
in map (\x->(fst $ f x,sort.snd $ f x))
. groupBy (\x y-> fst x == fst y ) . sort
main = do
print $ p2b [('C',3),('A',1),('B',2),('A',2),('C',2)]
print $ p2b $ map (\(x,y)->(map toUpper x,y))
[("bar",44),("Foo",2),("foo",22),("BAR",4)]
[bmaxa@maxa-pc WJ]$ ./pairs
[('A',[1,2]),('B',[2]),('C',[2,3])]
[("BAR",[4,44]),("FOO",[2,22])]
--
press any key to continue or any other to quit