# How to sort rooms by floor area

**URL:** <https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742>\
**Category:** scripting\
**Tags:** honeybee\
**Created:** [September 1, 2021, 8:56pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742 "2021-09-01T20:56:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gzorzeto](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/gzorzeto/32/40395_2.png) [@gzorzeto](https://discourse.ladybug.tools/u/gzorzeto)\
**Post date:** [September 1, 2021, 8:56pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/1 "2021-09-01T20:56:24Z")

</div>

Hello world!

My goal is pretty simple: I want to split my HB rooms in two lists, one containing the smallest room (by floor area) and the other list containing the rest. I already managed to do it with a GhPython script plus some native list components, as you can see bellow. However, I was wondering how to execute the whole task only by scripting. I tried to sort the x.floor\_area but I get a Runtime error saying that ‘float’ object has no attribut sort, although I though the results were arranged in a list.

 ![image](https://discourse.ladybug.tools/uploads/default/original/3X/7/0/7083bcc0fe7f89c5f8c6d7e206af1703f18d0cdf.png)

[Model sorting.gh](https://discourse.ladybug.tools/uploads/short-url/yyZOOPwbLkhZQV0Xx98YO8UNYFo.gh) (27.9 KB)

---

<div class="post-metadata">

**Author:** ![maheshjayayachander](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/maheshjayayachander/32/20460_2.png) [@maheshjayayachander](https://discourse.ladybug.tools/u/maheshjayayachander)\
**Post date:** [September 2, 2021, 8:11am UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/2 "2021-09-02T08:11:18Z")

</div>

@gzorzeto ,Try this.

 ![image](https://discourse.ladybug.tools/uploads/default/original/3X/1/c/1c249219b1cfb82119ddaf2908d3087829c16acb.png)

---

<div class="post-metadata">

**Author:** ![TrevorFedyna](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/trevorfedyna/32/15628_2.png) [@TrevorFedyna](https://discourse.ladybug.tools/u/TrevorFedyna)\
**Post date:** [September 2, 2021, 10:16pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/3 "2021-09-02T22:16:50Z")

</div>

Hi!  
if you want to do it in pure ghpy:

 ![image](https://discourse.ladybug.tools/uploads/default/original/3X/a/9/a92d80a03bc9a6ddea67b6fda2c1b2a893d61886.png)  
For copy/paste:

```python
# make a container for what we want to interogate
floor_area_list = []
# go through each list item and fill container with the desired
for room in _rooms:
    floor_area_list.append(room.floor_area)

# sort ascending:
a = sorted(floor_area_list)
# get smallest
# list items have an index from 0 -> end of list index
b = a[0]
# sort descending
c = sorted(floor_area_list, reverse = True)

print(floor_area_list)
print(a)
print(b)
print(c)

```

There is likely a way to do this with [list comprehension](https://www.w3schools.com/python/python_lists_comprehension.asp) as well.

Also Grasshopper uses python 2.7 (ironpython) which is a good bit different here and there from python 3.xx so add to any python related google python 2.7 and it should return better results for ghpy stuff.  
[This is a sorting with py 2.7 reference for sorting lists, I am not explicitly clear on if there is a way to .sort in 2.7 but this at least: does work for sure](https://cpython-test-docs.readthedocs.io/en/latest/howto/sorting.html)

best  
@TrevorFedyna

---

<div class="post-metadata">

**Author:** ![gzorzeto](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/gzorzeto/32/40395_2.png) [@gzorzeto](https://discourse.ladybug.tools/u/gzorzeto)\
**Post date:** [September 3, 2021, 1:16pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/4 "2021-09-03T13:16:05Z")

</div>

That’s pretty much what I was looking for, thank you for the help!

But just to complement your answer, it should be noted that we must choose “List Access” for \_rooms. Besides, since the initial goal was to get the rooms sorted by their floor\_area, I decided to sort them by doing [x for \_, x in sorted(zip(floor\_area\_list, \_rooms))].

Here is the edited code:

```auto
import rhinoscriptsyntax as rs
# make a container for what we want to interogate
floor_area_list = []
# go through each list item and fill container with the desired
for room in _rooms:
    floor_area_list.append(room.floor_area)

# sort ascending:
a = sorted(floor_area_list)
# get smallest
# list items have an index from 0 -> end of list index
b = a[0]
# sort descending
c = sorted(floor_area_list, reverse = True)
# sort rooms according to a (floor_area_list)
sorted_rooms = [x for _, x in sorted(zip(floor_area_list, _rooms))]

```

Cheers,  
Gustavo

---

<div class="post-metadata">

**Author:** ![mostapha](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mostapha/32/15244_2.png) [@mostapha](https://discourse.ladybug.tools/u/mostapha)\
**Post date:** [September 7, 2021, 7:55pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/5 "2021-09-07T19:55:01Z")

</div>

If I understand the original question correctly you are not looking for the area itself. You just want them sorted based on the area. In that case, I would use the `key` argument to make it easier. This single line should give you what you need:

 ![image](https://discourse.ladybug.tools/uploads/default/original/3X/e/3/e370f4db377432a0dc94feeecaa36da9dafe8752.png)

```python
a = sorted(x, key=lambda room: room.floor_area)

```

`x` should be set to List Access.

---

<div class="post-metadata">

**Author:** ![TrevorFedyna](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/trevorfedyna/32/15628_2.png) [@TrevorFedyna](https://discourse.ladybug.tools/u/TrevorFedyna)\
**Post date:** [September 8, 2021, 12:38am UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/6 "2021-09-08T00:38:27Z")

</div>

![image](https://discourse.ladybug.tools/uploads/default/original/3X/2/b/2b4e71428c9b4a23c0f62f9b5dc3faf25f51a179.jpeg)  
NICE!!

---

<div class="post-metadata">

**Author:** ![gzorzeto](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/gzorzeto/32/40395_2.png) [@gzorzeto](https://discourse.ladybug.tools/u/gzorzeto)\
**Post date:** [September 13, 2021, 8:24pm UTC](https://discourse.ladybug.tools/t/how-to-sort-rooms-by-floor-area/15742/7 "2021-09-13T20:24:00Z")

</div>

> [@mostapha](#):
>
> `key=lambda room: room.floor_area)`

What an elegant solution. Thank you a lot!
