# How does set.comparators.sort('sortBy') work?

**URL:** <https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236>\
**Category:** CanJS\
**Created:** [April 20, 2016, 8:19pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236 "2016-04-20T20:19:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pYr0x](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/pyr0x/32/95_2.png) [@pYr0x](https://forums.bitovi.com/u/pYr0x)\
**Post date:** [April 20, 2016, 8:19pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/1 "2016-04-20T20:19:35Z")

</div>

as a short describtion on [https://github.com/canjs/can-set](https://github.com/canjs/can-set)  
i really dont understand how set.comparators.sort(‘sortBy’)

here is a example: [http://jsbin.com/daqavakijo/2/edit?html,js,console,output](http://jsbin.com/daqavakijo/2/edit?html,js,console,output)

why is `dishes` not the first item?

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 20, 2016, 8:37pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/2 "2016-04-20T20:37:48Z")

</div>

> why is dishes not the first item?

It is the first item. But I think you mean “why is dishes the first item instead of agym?”.

The reason is that the sort operator does not actually sort the response from the server in this scenario. It expects the server did the sorting. The server is returning:

```auto
[
        {_id: 0, name: "dishes"},
        {_id: 3, name: "agym"}
      ]

```

So that’s the order items are added to the resulting list.

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 20, 2016, 8:40pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/3 "2016-04-20T20:40:57Z")

</div>

> The reason is that the sort operator does not actually sort the response from the server in this scenario.

In fact, for an initial `.getList(set)` , no filtering, sorting, pagination takes place. It assumes the server did all of that.

Only on other requests, assuming you are using `superMap` or its extensions like real-time or fall-through-cache, is the `set` used to make comparisons.

---

<div class="post-metadata">

**Author:** ![pYr0x](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/pyr0x/32/95_2.png) [@pYr0x](https://forums.bitovi.com/u/pYr0x)\
**Post date:** [April 20, 2016, 8:41pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/4 "2016-04-20T20:41:03Z")

</div>

sorry posted the wrong example  
here the updated: [http://jsbin.com/xutepuyuxi/1/edit?html,js,console,output](http://jsbin.com/xutepuyuxi/1/edit?html,js,console,output)

so if the sort.algebra does not sorting, why does it exists?

---

<div class="post-metadata">

**Author:** ![pYr0x](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/pyr0x/32/95_2.png) [@pYr0x](https://forums.bitovi.com/u/pYr0x)\
**Post date:** [April 20, 2016, 8:43pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/5 "2016-04-20T20:43:03Z")

</div>

> [@justinbmeyer](#):
>
> Only on other requests, assuming you are using superMap or its extensions like real-time or fall-through-cache, is the set used to make comparisons.

can you give me a example in jsbin?

this example with a new todo didnt work: [JS Bin - Collaborative JavaScript Debugging](http://jsbin.com/lupatadisu/1/edit?html,js,console,output)

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 21, 2016, 2:06am UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/6 "2016-04-21T02:06:17Z")

</div>

It exists for extensions that make use of getSubset or getUnion or index.

In bitballs, when a new player is created, it’s added in its sorted place in the list of players.

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 21, 2016, 2:08am UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/7 "2016-04-21T02:08:53Z")

</div>

It didn’t work because you didn’t request a sorted list from the server. You passed it {}. The default sorting will then be by the id prop.

---

<div class="post-metadata">

**Author:** ![pYr0x](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/pyr0x/32/95_2.png) [@pYr0x](https://forums.bitovi.com/u/pYr0x)\
**Post date:** [April 21, 2016, 7:28am UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/8 "2016-04-21T07:28:47Z")

</div>

@justinbmeyer can you make my example working?  
i am still not understand the behavoir

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 21, 2016, 2:45pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/9 "2016-04-21T14:45:21Z")

</div>

I just changed it to:

`Todo.getList({sortBy: "name"})`

http://jsbin.com/galetab/embed?html,js,console,output

---

<div class="post-metadata">

**Author:** ![cherif\_b](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/cherif_b/32/31_2.png) [@cherif\_b](https://forums.bitovi.com/u/cherif_b)\
**Post date:** [April 25, 2016, 4:50pm UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/10 "2016-04-25T16:50:51Z")

</div>

@justinbmeyer so the `sortBy` will be send as query param? like [myhost.biz/todos?sortBy=name](http://myhost.biz/todos?sortBy=name)

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [April 26, 2016, 12:02am UTC](https://forums.bitovi.com/t/how-does-set-comparators-sort-sortby-work/236/11 "2016-04-26T00:02:34Z")

</div>

yes, that is what will happen.
