我在淘汰赛中有一个Observable数组,里面有数组,每个数组中都有一个称为“时间”的项目。我将如何在可观察数组中对数组进行排序,以便根据项的值从低到高的顺序排列。值采用小时:分钟:第二格式。
function viewModel() {
var self = this;
self.myObservable = ko.observableArray([
{time: "01:00:00", name: 'frank'},
{time: "05:30:00", name: 'bob'},
{time: "03:00:00", name: 'bill'},
{time: "00:00:21", name: 'john'},
{time: "00:12:00", name: 'paul'}
])
}
答案 0 :(得分:0)
您应该使用sort()函数。
self.myObservable.sort(function (left, right) {
return left.age === right.age ? 0
: left.age < right.age ? -1
: 1;
});
答案 1 :(得分:0)
淘汰赛的Income
有built-in methods,其工作方式与Array.prototype
类似。您可以使用sort
localeCompare
observableArray
function viewModel() {
var self = this;
self.myObservable = ko.observableArray([
{ time: "01:00:00", name: 'frank' },
{ time: "05:30:00", name: 'bob' },
{ time: "03:00:00", name: 'bill' },
{ time: "00:00:21", name: 'john' },
{ time: "00:12:00", name: 'paul' }
]);
self.sort = function() {
self.myObservable.sort((a, b) => a.time.localeCompare(b.time))
}
}
ko.applyBindings(new viewModel)