Reputation:
I have this complicated type
newtype CalendarDay = MkCal (Either AvailableDay UnAvailableDay) deriving (Show,Ord,Eq)
Which is made up of
newtype AvailableDay = MkAD (Text, C.Day) deriving (Show,Eq,Ord)
and
newtype UnAvailableDay = MkUAD (Either ScheduledDay (C.Day, Out_Of_Office)) deriving (Show,Ord,Eq)
ScheduledDay looks like this
data ScheduledDay = MkSDay C.Day Product ScheduledState deriving (Show,Ord,Eq)
I will have a [CalendarDay], that I want to sort by the Day that is buried in all those other types. What will I have to do to make that happen?
Upvotes: 1
Views: 103
Reputation: 139840
Define a function day :: CalendarDay -> Day
which extracts the day from your type. You can do this with your current types, but as I mentioned in my comments, I would recommend refactoring those first. It should then be a simple matter of doing some pattern matching to extract the Day
field from your type.
Once you have this function, use sortBy
from Data.List
to sort using a custom comparison function. You can build one with comparing
from Data.Ord
, like so:
sortBy (comparing day) foo
where foo :: [CalendarDay]
is your list of calendar days.
Upvotes: 3