# Sort list by given pattern

**URL:** <https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642>\
**Category:** Grasshopper\
**Created:** [April 28, 2018, 11:26am UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642 "2018-04-28T11:26:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainerhartl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rainerhartl/32/209874_2.png) [@rainerhartl](https://discourse.mcneel.com/u/rainerhartl)\
**Post date:** [April 28, 2018, 11:26am UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/1 "2018-04-28T11:26:10Z")

</div>

![sort%20list](https://global.discourse-cdn.com/mcneel/original/3X/3/2/3237961c894c42f504c7ec74f6d00d90a7ebcd41.JPG)

Hi there,

How can I sort a List by a specific pattern like in the image?  
value with index 0 from list 1 goes to List 2 and replace value 0  
value with index 1 from list 1 goes to List 2 and replace value 1  
an so on…

Thanks

---

<div class="post-metadata">

**Author:** ![HS\_Kim](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/hs_kim/32/473975_2.png) [@HS\_Kim](https://discourse.mcneel.com/u/HS_Kim)\
**Post date:** [April 28, 2018, 1:17pm UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/2 "2018-04-28T13:17:38Z")

</div>

You want **`List Item`**?

---

<div class="post-metadata">

**Author:** ![rainerhartl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rainerhartl/32/209874_2.png) [@rainerhartl](https://discourse.mcneel.com/u/rainerhartl)\
**Post date:** [April 29, 2018, 6:23am UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/3 "2018-04-29T06:23:58Z")

</div>

No, i want the left list look like the right list. With all its branches and all the items in correct order.

---

<div class="post-metadata">

**Author:** ![qythium](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/qythium/32/84125_2.png) [@qythium](https://discourse.mcneel.com/u/qythium)\
**Post date:** [April 29, 2018, 8:22am UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/4 "2018-04-29T08:22:55Z")

</div>

Simple enough with a bit of scripting 🙂

```auto
d = dict(enumerate(xs))
a = [d[y] for y in ys]

```

(Be sure to set both inputs to List Access)

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/e/e/ee2c15ca5e6b2ba0661213d2edebc080dabecad4.png)

---

<div class="post-metadata">

**Author:** ![HS\_Kim](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/hs_kim/32/473975_2.png) [@HS\_Kim](https://discourse.mcneel.com/u/HS_Kim)\
**Post date:** [April 29, 2018, 9:14am UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/5 "2018-04-29T09:14:49Z")

</div>

![Canvas%20at%2018%3B13%3B49](https://global.discourse-cdn.com/mcneel/original/3X/0/1/01ffc3717cb702e4c1a0306998bb9ede8f575a3f.png)

---

<div class="post-metadata">

**Author:** ![qythium](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/qythium/32/84125_2.png) [@qythium](https://discourse.mcneel.com/u/qythium)\
**Post date:** [April 29, 2018, 2:26pm UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/6 "2018-04-29T14:26:46Z")

</div>

Oops, I guess that works as well 😅  
Anything involving list/tree matching using indices and I instinctively fall back on writing a one-or two liner script - looks like that wasn’t needed here!

Edit: Actually looking back at the code, it could be simplified to:

```auto
a = [xs[y] for y in ys]

```

at which point replacing it with List Item looks more obvious

---

<div class="post-metadata">

**Author:** ![rainerhartl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rainerhartl/32/209874_2.png) [@rainerhartl](https://discourse.mcneel.com/u/rainerhartl)\
**Post date:** [April 29, 2018, 5:27pm UTC](https://discourse.mcneel.com/t/sort-list-by-given-pattern/61642/7 "2018-04-29T17:27:07Z")

</div>

List Item!?  
Thanks a lot,the solution for that seemed to be so far away altough so close! And thanks as well for the introduction in scripting! 😀
