## SOLVED: Haskell: function to sort 1st element of 3 tuples lexicographically

henahzurfdsa:

Suppose we have a spreadsheet :: [(String,String,Int)] containing triples (name, user,mark) where name is the lastname of a student, user is his username and mark is the result of the student’s first CW. Write a function sortLastname that sorts a spreadsheet lexicographically by names using the higher order insertion sort defined in Exercise 2.

My code for exercise 2:

``````homerge :: Ord b => (a -> b) -> [a] -> [a] -> [a]
homerge _ xs [] = xs
homerge _ [] ys = ys
homerge fun (x:xs) (y:ys) | fun x < fun y = x : homerge fun xs (y:ys)
| otherwise = y : homerge fun (x:xs) ys`

hoMergeSort :: Ord b => (a -> b) -> [a] -> [a]
hoMergeSort _ [x] = [x]
hoMergeSort fun xs = homerge fun (hoMergeSort fun ys) (hoMergeSort fun ws)
where (ys,ws) = (take l xs, drop l xs)
where l = length xs `div` 2
```
```

My current attempt:

``````sortLastName [(x,y,z)] = hoMergeSort ( (x,_,_) (x',_,_) = x<x' ) [(x,y,z)]
```
```

My problem is getting the correct function to check whether the first element of 3-tuple is less than the next element.

Any advice would be really appreciated.

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots