# How to align/sort two maps

**URL:** <https://discuss.hashicorp.com/t/how-to-align-sort-two-maps/13780>\
**Category:** Terraform\
**Created:** [September 4, 2020, 6:09am UTC](https://discuss.hashicorp.com/t/how-to-align-sort-two-maps/13780 "2020-09-04T06:09:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crdiaz324](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/crdiaz324/32/9955_2.png) [@crdiaz324](https://discuss.hashicorp.com/u/crdiaz324)\
**Post date:** [September 4, 2020, 6:09am UTC](https://discuss.hashicorp.com/t/how-to-align-sort-two-maps/13780/1 "2020-09-04T06:09:11Z")

</div>

Does anyone know how to solve this little terraform challenge? I can’t come up with a solution for this. I have two maps like this:

instances = {  
“instance1” = “us-east-1a”  
“instance2” = “us-east-1a”  
“instance3” = “us-east-1c”  
“instance4” = “us-east-1b”  
“instance5” = “us-east-1b”  
“instance6” = “us-east-1c”  
}

snapshots = {  
“snap1” = “us-east-1c”  
“snap2” = “us-east-1b”  
“snap3” = “us-east-1b”  
“snap4” = “us-east-1c”  
“snap5” = “us-east-1a”  
“snap6” = “us-east-1a”  
}

What I would like to get out of this is:  
desired\_result = {  
“instance1” = “snap5”  
“instance2” = “snap6”  
“instnace3” = “snap1”  
“instance4” = “snap2”  
“instance5” = “snap3”  
“instance6” = “snap4”  
}

Any ideas would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![crdiaz324](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/crdiaz324/32/9955_2.png) [@crdiaz324](https://discuss.hashicorp.com/u/crdiaz324)\
**Post date:** [September 12, 2020, 4:07pm UTC](https://discuss.hashicorp.com/t/how-to-align-sort-two-maps/13780/2 "2020-09-12T16:07:21Z")

</div>

I was finally able to solve this problem. I did a quick writeup on how I did this on stackoverflow in case anyone else runs into a similar issue: [https://stackoverflow.com/questions/63735853/how-to-sort-merge-two-maps-in-terraform/63862185](https://stackoverflow.com/questions/63735853/how-to-sort-merge-two-maps-in-terraform/63862185)

It may not be the cleanest solution, but it accomplishes my goal. If anyone else has suggestions on how to improve the logic or make it more efficient, please do let me know.
