From ea55dd58e112478b68d86b94f6b3601975886706 Mon Sep 17 00:00:00 2001 From: Mike Mitchel Date: Wed, 31 Oct 2018 10:46:06 -0500 Subject: [PATCH] add basic sort method to can-list --- can-list.js | 6 ++++++ can-list_test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/can-list.js b/can-list.js index dc68d00..f876c2d 100644 --- a/can-list.js +++ b/can-list.js @@ -762,6 +762,12 @@ assign(List.prototype, { }); return filteredList; + }, + sort: function(compareFunction) { + var sorting = Array.prototype.slice.call(this); + Array.prototype.sort.call(sorting, compareFunction); + this.splice.apply(this, [0,sorting.length].concat(sorting) ); + return this; } }); diff --git a/can-list_test.js b/can-list_test.js index 66b5928..290262c 100644 --- a/can-list_test.js +++ b/can-list_test.js @@ -594,3 +594,49 @@ QUnit.test("can.onInstanceBoundChange basics", function(){ [people, false ] ]); }); + +QUnit.test('list.sort a simple list', function() { + var myList = new List([ + "Marshall", + "Austin", + "Hyrum" + ]); + + myList.sort(); + + equal(myList.length, 3); + equal(myList[0], "Austin"); + equal(myList[1], "Hyrum"); + equal(myList[2], "Marshall", "Basic list was properly sorted."); +}); + +QUnit.test('list.sort a list of objects', function() { + var objList = new List([ + { id: 1, name: "Marshall" }, + { id: 2, name: "Austin" }, + { id: 3, name: "Hyrum" } + ]); + + objList.sort(function(a, b) { + if (a.name < b.name) { + return -1; + } else if (a.name > b.name) { + return 1; + } else { + return 0; + } + }); + + equal(objList.length, 3); + equal(objList[0].name, "Austin"); + equal(objList[1].name, "Hyrum"); + equal(objList[2].name, "Marshall", "List of objects was properly sorted."); +}); + +QUnit.test('list.sort a list of objects without losing reference (#137)', function() { + var unSorted = new List([ { id: 3 }, { id: 2 }, { id: 1 } ]); + var sorted = unSorted.slice(0).sort(function(a, b) { + return a.id > b.id ? 1 : (a.id < b.id ? -1 : 0); + }); + equal(unSorted[0], sorted[2], 'items should be equal'); +});