# Poll: should default sort behavior be "fast" or "stable"?

**URL:** <https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360>\
**Category:** Crystal Contrib\
**Created:** [November 14, 2019, 2:23am UTC](https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360 "2019-11-14T02:23:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [November 14, 2019, 2:23am UTC](https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360/1 "2019-11-14T02:23:10Z")

</div>

Hello.

Related to [https://github.com/crystal-lang/crystal/issues/6057](https://github.com/crystal-lang/crystal/issues/6057)

Today Crystal’s only option for sorting is “unstable” but fast quicksort.

This means that things like ary.sort\_by(&.x).sort\_by(&.x) don’t actually make sense, the second sort\_by scrambles what the first did. Java gets around this by sorting “stably” by default.

There’s also some “other” sort algorithms that take up slightly more space but are “faster” than quicksort.

So the question of the day is…what should the default sort behavior best be? Fast? Stable? Even faster but using more memory? Stable for just sort\_by?

Thoughts and opinions welcome here! I just wasn’t sure if crystal focuses more on “ease” to the programmer, or more on “speed” by default, so asking for a poll here to learn more. Thanks!

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [November 14, 2019, 2:26am UTC](https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360/2 "2019-11-14T02:26:08Z")

</div>

For me, preferred default would be “easiest for the programmer” (i.e. stable). Make the rest optional, for cases when people have profiled, and found they need speed and understand the consequences… :)  
You can even have stable for object sorts, but “unstable” for integer sort (etc.) since re-ordering doesn’t matter… :). Actually my preference for integer sorts would be “as fast as possible by default” since reordering doesn’t matter. Default to the fastest :)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 14, 2019, 9:59am UTC](https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360/3 "2019-11-14T09:59:57Z")

</div>

Stable. Crystal should always put sanity of mind first. Performance doesn’t help if the result is incorrect or the correct result is hard to achieve.

Adding optimization for hot paths is always possible, but needs to be opt-in.

---

<div class="post-metadata">

**Author:** ![drosehn](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drosehn/32/72_2.png) [@drosehn](https://forum.crystal-lang.org/u/drosehn)\
**Post date:** [November 14, 2019, 8:56pm UTC](https://forum.crystal-lang.org/t/poll-should-default-sort-behavior-be-fast-or-stable/1360/4 "2019-11-14T20:56:09Z")

</div>

I’d prefer stable-sorting by default.
