# Problems sorting initial data donejs+mongodb+feathersjs

**URL:** <https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855>\
**Category:** DoneJS\
**Created:** [April 14, 2018, 9:33pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855 "2018-04-14T21:33:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nriesco](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/nriesco/32/190_2.png) [@nriesco](https://forums.bitovi.com/u/nriesco)\
**Post date:** [April 14, 2018, 9:33pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/1 "2018-04-14T21:33:52Z")

</div>

I have issues sorting the data received by a REST API.

I created a very simple feathersjs project ([https://github.com/nriesco/donejs-sorter-problem-api](https://github.com/nriesco/donejs-sorter-problem-api)), you’ll need a DB called `donejs_sorter_problem_api` a collection named `demo` and this sample data:

```auto
{
    "_id" : ObjectId("5a9ca44b75cc838af37e57f4"),
    "name" : "Camila",
    "order" : 10
}, {
    "_id" : ObjectId("5a9ca45b75cc838af37e57f5"),
    "name" : "Antonia",
    "order" : 2
}, {
    "_id" : ObjectId("5a9ca47075cc838af37e57f6"),
    "name" : "Ignacia",
    "order" : 3
}

```

Then start it using `npm start` and test it here: [http://localhost:4000/demo](http://localhost:4000/demo)

Then using a donejs project ([https://github.com/nriesco/donejs-sorter-problem](https://github.com/nriesco/donejs-sorter-problem)) go to the home page and you’ll see the not-sorted elements. You can modify these lines (invert the comments) [https://github.com/nriesco/donejs-sorter-problem/blob/master/src/demo/demo.js#L35-L36](https://github.com/nriesco/donejs-sorter-problem/blob/master/src/demo/demo.js#L35-L36) and the browser will freeze because it uses 100% cpu (an infinite loop).

I tried not sorting when it was not a DefineMap instance, and when using SSR but no luck at all.

Thanks

---

<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 14, 2018, 9:40pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/2 "2018-04-14T21:40:31Z")

</div>

I’m betting this will break without ssr, in plain canjs. This is because sort in JS mutates the array. Instead, could you clone the define list and sort the resulting array?

list.get().sort(…)

---

<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 14, 2018, 9:47pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/3 "2018-04-14T21:47:03Z")

</div>

This is something canjs 4 can warn about. We can see mutations while a compute is evaluating and warn. Are you using 4.0? I can show where to put this warning.

---

<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 14, 2018, 10:19pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/4 "2018-04-14T22:19:24Z")

</div>

Another idea would be to turn off mutations as side effects within derive entirely. The sort would happen, but would produce no events.

---

<div class="post-metadata">

**Author:** ![nriesco](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/nriesco/32/190_2.png) [@nriesco](https://forums.bitovi.com/u/nriesco)\
**Post date:** [April 15, 2018, 8:28pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/5 "2018-04-15T20:28:26Z")

</div>

I use canjs 3.

I tried this but shows no results:

```auto
sortedElements: {
    get: function() {
      if (this.elements !== undefined) {
        let out = this.elements.map(element => {
          element = new DefineMap({ seal: false }, element);
          // element = Object.assign({}, element);
        });
        return new DefineList(out);
      } else {
        return this.elements;
      }
      // if (platform.isNode) {
      // return this.elements;
      // } else {
      // if (this.elements instanceof DefineList) {
      // // returning this.elements.sort() will cause the browser to freeze (infinite loop)
      // // return this.elements.sort(sorter);
      // return this.elements;
      // } else {
      // return this.elements;
      // }
      // }
    }
  },

```

---

<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 15, 2018, 10:36pm UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/6 "2018-04-15T22:36:46Z")

</div>

Discussion here: [https://gitter.im/donejs/donejs?at=5ad274153fe1be3704cabfb4](https://gitter.im/donejs/donejs?at=5ad274153fe1be3704cabfb4)

Solution is to clone the data you are modifying as shown in this JSBin: [http://jsbin.com/gakenah/edit?html,js,output](http://jsbin.com/gakenah/edit?html,js,output)

```auto
  modifiedElements: {
    get: function() {
      if (this.elements !== undefined) {
        var res = this.elements.get().sort(function(a,b){
          return -1;
        });
        return res;
      }
    }
  },

```

---

<div class="post-metadata">

**Author:** ![nriesco](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/nriesco/32/190_2.png) [@nriesco](https://forums.bitovi.com/u/nriesco)\
**Post date:** [April 16, 2018, 1:20am UTC](https://forums.bitovi.com/t/problems-sorting-initial-data-donejs-mongodb-feathersjs/855/7 "2018-04-16T01:20:32Z")

</div>

Problem solved, thanks to @justinbmeyer.  
I’ve updated [https://github.com/nriesco/donejs-sorter-problem](https://github.com/nriesco/donejs-sorter-problem)  
The whole viewmodel looks llike this:

```auto
export const ViewModel = DefineMap.extend({
  get myPromise() {
    return DemoModel.getList({});
  },
  elements: {
    get: function(last, set) {
      this.myPromise.then(set);
    }
  },
  sortedElements: {
    get: function() {
      if (this.elements !== undefined) {
        return this.elements.get().sort(sorter);
      }
    }
  },
  modifiedElements: {
    get: function() {
      if (this.elements !== undefined) {
        return this.elements.get().map(element => {
          element.order += ' super!';
          return element;
        });
      }
    }
  },
});

```

More details here: [https://github.com/nriesco/donejs-sorter-problem/blob/master/src/demo/demo.js#L40-L57](https://github.com/nriesco/donejs-sorter-problem/blob/master/src/demo/demo.js#L40-L57)
