sort an array based on an array of another/different type in haskell or purescript

188 Views Asked by At

I have two Arrays of different types, 1) say moduleinfo array which has each element of type

{name :: String, pack :: String }

and 2) String Array. The simple String array contains element that is similar to the name field of moduleinfo, i.e String. The simple String array is already sorted but not the moduleinfo array.

How do I write a function to sort the moduleinfo array based on the order of simple String array. Given that the name field in moduleinfo would match with simple String array elements.

The function shall return a sorted array of moduleinfo type.

1

There are 1 best solutions below

0
On

I suggest that you sort moduleinfo directly:

sortedModuleInfo = sortWith (\(Module name1 pack1) (Module name2 pack2) -> compare name1 name2) moduleinfo

Here I assume that you have declared a type named Module with the name and pack elements which you showed in your question.