How to use CANJS sort() function

318 Views Asked by At

I went through the CANJS documentation and tried to implement the sort function on can.List().

var list = new can.List([
    { name: 'Justin' },
    { name: 'Brian' },
    { name: 'Austin' },
    { name: 'Mihael' }])

list.comparator = 'name';
list.sort(); //- sorts the list by the name attribute

When i tried to run this on console(browser's). It is saying "Uncaught TypeError: undefined is not a function ". Why? Am i missing something here?

Reference: http://canjs.com/docs/can.List.prototype.sort.html

2

There are 2 best solutions below

2
On BEST ANSWER

Sorting a list requires the can.list.sort plugin in addition to base canjs. You can download it from http://canjs.com/release/latest/can.list.sort.js

0
On

If you are running this in a browser console you have to make sure can is loaded. You can check by typing can in the console, it should return an object of functions, etc. Then if it is there, you should make sure that List was included in your version of Can, if you built it by hand.