I have two lists ["a","b","c","d"] and ["b","d","a","c"]
How can I make a function that orders the first list with the same order of the second one?
In this example something like this:
> ord ["a","b","c","d"] ["b","d","a","c"]
["b","d","a","c"]
all the function I make give me an incomplete list:
ord :: [String] -> [String] -> [String]
ord [] _ = []
ord (h:t) (x:xs) | (h==x) = h:(ord t xs)
| otherwise = ord t (x:xs)
This is only an example; I can’t simply present the second list.
Here’s a quick and dirty solution that builds the result by grouping each string in the first list by the order in the second (I also renamed
ordtoorderThese):As an example,
orderThese ["a", "c", "a", "b"] ["b", "a", "c"]returns["b","a","a","c"].