Skip to content

Commit

Permalink
Merge pull request #86 from canjs/85-sort-method
Browse files Browse the repository at this point in the history
add basic sort method to can-list
  • Loading branch information
mikemitchel authored Oct 31, 2018
2 parents 88bf1a8 + ea55dd5 commit 40e55ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions can-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
46 changes: 46 additions & 0 deletions can-list_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 40e55ca

Please sign in to comment.