# Find the nearest value in an Elm list

> This example is not exhaustive nor performant, but allows you to find the nearest value in an Elm list. A short Today I Learned note from the Graffino team.

- URL: https://graffino.com/til/a-naive-approach-to-finding-the-nearest-value-in-an-elm-list
- Author: Nick Ciolpan
- Channel: elm
- Published: 2020-07-20
- Updated: 2024-01-30

```
closest: Int -> List Int -> Int
closest size sizes =
  sizes
      |> List.sort
      |> List.filter (\s -> s < size)
      |> List.reverse
      |> List.head
      |> Maybe.withDefault 0
```

This example is not meant to be exhaustive nor performant.
