preferLinked :: Tree a -> Tree a
preferLinked = trav go
  where
    go (PChoiceF qn a  cs) = PChoiceF qn a (P.sortByKeys cmp cs)
    go x                   = x

    cmp (POption _ linkedTo) (POption _ linkedTo') = cmpL linkedTo linkedTo'

    cmpL Nothing  Nothing  = EQ
    cmpL Nothing  (Just _) = GT
    cmpL (Just _) Nothing  = LT
    cmpL (Just _) (Just _) = EQ


-- | Ordering that treats versions satisfying more preferred ranges as greater
--   than versions satisfying less preferred ranges.
preferLinked :: Tree a -> Tree a
preferLinked = trav go
  where
    go (PChoiceF qn a  cs) = PChoiceF qn a (P.sortByKeys cmp cs)
    go x                   = x

    cmp (POption _ linkedTo) (POption _ linkedTo') = cmpL linkedTo linkedTo'

    cmpL Nothing  Nothing  = EQ
    cmpL Nothing  (Just _) = GT
    cmpL (Just _) Nothing  = LT
    cmpL (Just _) (Just _) = EQ


-- | Ordering that treats versions satisfying more preferred ranges as greater
--   than versions satisfying less preferred ranges.
preferLinked = trav go
  where
    go (PChoiceF qn a  cs) = PChoiceF qn a (P.sortByKeys cmp cs)
    go x                   = x

    cmp (POption _ linkedTo) (POption _ linkedTo') = cmpL linkedTo linkedTo'

    cmpL Nothing  Nothing  = EQ
    cmpL Nothing  (Just _) = GT
    cmpL (Just _) Nothing  = LT
    cmpL (Just _) (Just _) = EQ


-- | Ordering that treats versions satisfying more preferred ranges as greater
--   than versions satisfying less preferred ranges.