# How to sort an Array of NamedTuples?

**URL:** https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663
**Category:** Help & Support
**Created:** [January 31, 2020, 12:41am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663 "2020-01-31T00:41:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ejstembler](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ejstembler/32/553_2.png) [@ejstembler](https://forum.crystal-lang.org/u/ejstembler)
#### Post date: [January 31, 2020, 12:41am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663/1 "2020-01-31T00:41:01Z")

</div>

I have an Array of NamedTuples I’m trying to sort:

```crystal
post_images : Array({slug: String, filename: String}) = Array({slug: String, filename: String}).new

# << items appended here

post_images.sort_by! { |i| i[:filename] } # This doesn't work

post_images.sort! { |a, b| a[:filename] <=> b[:filename] } # Neither does this

```

Here’s the error:

```auto
199 | v = v1 <=> v2
              ^--
Error: undefined method '<=>' for NamedTuple(slug: String, filename: String)

```

Am I missing something simple?

Incidentally, this was the first thing I tried:

```crystal
post_images.sort_by! { |i| i.filename }

```

```auto
51 | post_images.sort_by! { |i| i.filename }
                                   ^-------
Error: undefined method 'filename' for NamedTuple(slug: String, filename: String)

```

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [January 31, 2020, 12:59am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663/2 "2020-01-31T00:59:00Z")

</div>

`post_images.sort_by! { |i| i[:filename] }` works fine tho?

[https://play.crystal-lang.org/#/r/8hbe](https://play.crystal-lang.org/#/r/8hbe)

EDIT: Probably would be better to define a struct for this? Then you could include `Comparable` and define the `<=>` which would allow you to just call `.sort` on the array.

---

<div class="post-metadata">

### Author: ![ejstembler](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ejstembler/32/553_2.png) [@ejstembler](https://forum.crystal-lang.org/u/ejstembler)
#### Post date: [January 31, 2020, 1:09am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663/3 "2020-01-31T01:09:49Z")

</div>

It’s weird that I’m getting that error. I changed your snippet to append `<<` to see if that was my issue, but it works.

One difference I didn’t post was that my function’s return type is really nilable:

```crystal
def post_banner_images(logger : ColorizedLogHandler, post : Post, size : (NamedTuple(width: Int32, height: Int32) | Nil) = nil) : Array({slug: String, filename: String})?
end

```

But the branch inside is guaranteed to be not null where I’m appending and trying to sort before returning. My only guess is the compiler doesn’t like that?

> Probably would be better to define a struct for this? Then you could include Comparable and define the \<=\> which would allow you to just call .sort on the array.

Yeah, I do that exactly thing elsewhere in my code base where I’m defining a Post class and Author class. However, for this little post images I thought I just use a throw-away NamedTuple instead…

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [January 31, 2020, 1:13am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663/4 "2020-01-31T01:13:36Z")

</div>

> [@ejstembler](#):
>
> But the branch inside is guaranteed to be not null where I’m appending and trying to sort before returning. My only guess is the compiler doesn’t like that?

Can you make a playground link with an example where its not working?

---

<div class="post-metadata">

### Author: ![ejstembler](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ejstembler/32/553_2.png) [@ejstembler](https://forum.crystal-lang.org/u/ejstembler)
#### Post date: [January 31, 2020, 5:02am UTC](https://forum.crystal-lang.org/t/how-to-sort-an-array-of-namedtuples/1663/5 "2020-01-31T05:02:20Z")

</div>

I couldn’t, a simplified version of the function worked in the playground as well.

That lead me down the path of using ` --error-trace` to get more details… It turned out that I forgot to delete another reference I had before moving the sort to the function. That other original attempt at sort was causing the problem.

Thanks for your help!
