# “This is not a record”-Error when trying to sort a list

**URL:** https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219
**Category:** Learn
**Created:** [February 26, 2019, 8:37pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219 "2019-02-26T20:37:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Caturix99](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@Caturix99](https://discourse.elm-lang.org/u/Caturix99)
#### Post date: [February 26, 2019, 8:37pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/1 "2019-02-26T20:37:36Z")

</div>

I want to sort my list descended but when I use the keyword .sort I get an error:

```auto
This is not a record, so it has no fields to access!

106| { model | teams = List.sort.map (\p -> {p | activated = True}) model.teams, activatedOutput = True} 
                             ^^^^^^^^^
This `sort` value is a:

    List comparable -> List comparable

But I need a record with a map field!

```

This is the line I have modified and with which I want to sort the list.

```elm
    Submit -> 
          { model | teams = List.sort.map (\p -> {p | activated = True}) model.teams, activatedOutput = True} 

```

---

<div class="post-metadata">

### Author: ![klaftertief](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/klaftertief/32/5338_2.png) [@klaftertief](https://discourse.elm-lang.org/u/klaftertief)
#### Post date: [February 26, 2019, 9:10pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/2 "2019-02-26T21:10:02Z")

</div>

`sort` is not a keyword, it is a function that takes a list of comparable items and returns a new list of the sorted comparable items. You can’t combine it like that with the `List.map` function.  
What you can do is to create a pipeline of functions that each use take and return a `List`, like  
`{ model | teams = model.teams |> List.sort |> List.map (\p -> {p | activated = True}) }`.

This wont like that directly, because your `teams` List contains records, which are not comparable. There is `List.sortBy` to the rescue, see [https://package.elm-lang.org/packages/elm/core/latest/List#sortBy](https://package.elm-lang.org/packages/elm/core/latest/List#sortBy)  
You would use it in pipeline style like that  
`{ model | teams = model.teams |> List.sortBy (\p -> tooSomeComparableValue p) |> List.map (\p -> {p | activated = True}) }`.

---

<div class="post-metadata">

### Author: ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)
#### Post date: [February 26, 2019, 11:57pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/3 "2019-02-26T23:57:59Z")

</div>

There is also a nice shortcut version when sorting on a key in record:

```auto
model.persons
  |> List.sortBy .lastName

```

---

<div class="post-metadata">

### Author: ![Caturix99](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@Caturix99](https://discourse.elm-lang.org/u/Caturix99)
#### Post date: [February 27, 2019, 12:04pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/4 "2019-02-27T12:04:16Z")

</div>

So if these are my records and I want to sort it for players. I should insert “player” for “tooSomeComparableValue”. But if I do so I get an error

```
I cannot find a `player` variable:

119| { model | teams = model.teams |> List.sortBy (\p -> player p) |> List.map (\p -> {p | activated = True}) } 
                                                               ^^^^^^
These names seem close though:

    poster
    always
    clamp
    class

Hint: Read <https://elm-lang.org/0.19.0/imports> to see how `import`
declarations work in Elm.

```

Code

```
 -- MODEL
    type alias Player =
      { player : String
      , strength : Int
      --, number : Int
      --, playernumber : Int
      --, placeholder : String
      --, counter : Int
      , activated : Bool
      }

    type alias Model =
      { content : String
      , teams : List Player
      , currentNumber : Int
      , currentPlayernumber: Int
      , currentPlayer : String
      , currentStrength : Int
      , placeholderPlayer : String
      , placeholderCounter : Int
      , placeholderStrength: Int
      , activatedOutput : Bool
      }

  

init : Model
init =
  { content = ""
  , teams = []
  , currentNumber = 0
  , currentPlayernumber = 0
  , currentPlayer = ""
  , currentStrength = 0
  , placeholderPlayer = ""
  , placeholderCounter = 1
  , placeholderStrength = 0
  , activatedOutput = False

   }
```

---

<div class="post-metadata">

### Author: ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)
#### Post date: [February 27, 2019, 12:28pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/5 "2019-02-27T12:28:55Z")

</div>

`p.player` rather than `player p` is the correct syntax here, so you can use @Atlewee’s suggestion.

Since `teams : List Player`, `p` in this context is one `Player` in the list.

---

<div class="post-metadata">

### Author: ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)
#### Post date: [February 27, 2019, 2:17pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/6 "2019-02-27T14:17:36Z")

</div>

> [@Caturix99](#):
>
> { model | teams = model.teams |\> List.sortBy (\p -\> player p) |\> List.map (\p -\> {p | activated = True}) }

The correct one is:

```auto
{ model | teams = model.teams |> List.sortBy (\p -> p.player) |> List.map (\p -> {p | activated = True}) }

```

You should probably find better names… Its easy to get confuesd when both the record and the field is refered to as player…

And it can be made more readable like this:

```auto
{ model | teams = 
  model.teams 
    |> List.sortBy .player -- (This is sorting by player.player)
    |> List.map (\playerRecord -> { playerRecord | activated = True } )
}

```

You can also make it ultra readable like this:

```auto
activatePlayer : Player -> Player
activatePlayer p =
    { p | activated = True } 

model.teams 
    |> List.sortBy .player
    |> List.map activatePlayer

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [March 9, 2019, 2:17pm UTC](https://discourse.elm-lang.org/t/this-is-not-a-record-error-when-trying-to-sort-a-list/3219/7 "2019-03-09T14:17:39Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
