在 NativeScript 中对 ListView 进行排序
Sort ListView in NativeScript
如何在 NativeScript 中从早到晚对数组进行排序?
我的代码:
import observableArray = require("data/observable-array");
var orderListRaw = new observableArray.ObservableArray([]);
var orderList = orderListRaw.sort((a:number, b:number) => {return a - b;});
function news(args) {
fetch('https://testapi.local/', {
method: 'GET',
}).then(response => {
return response.json();
}).then(function(r) {
var res = r.results;
orderList.length = 0
console.log(JSON.stringify(res));
for (var i = 0; i < res.length; i++) {
var order = {
createdAt: res[i].createdAt,
title: res[i].title,
desc: res[i].desc,
image: res[i].image.url
}
orderList.push(order);
}
},
function(e) {
console.log(e);
});
};
结果是,我的数组仍然按照从早到晚的顺序排序。
非常感谢
试试这个:
var moment = require("moment");
orderList.sort((left: any, right: any) => {
return moment(left.createdAt).isBefore(right.createdAt) ? -1 : 1;
});
您可以使用比较函数对 ObsevableArray 进行排序,但您应该在数组有一些实际值被推入和推入后使用它们:
orderList.sort(function (orderA, orderB) {
var createdAtA = orderA.createdAt; // what is createdAt!? probably normilize it for comparision
var createdAtB = orderB.createdAt;
return (createdAtA < createdAtB ) ? -1 : (createdAtA > createdAtB ) ? 1 : 0;
});
如何在 NativeScript 中从早到晚对数组进行排序?
我的代码:
import observableArray = require("data/observable-array");
var orderListRaw = new observableArray.ObservableArray([]);
var orderList = orderListRaw.sort((a:number, b:number) => {return a - b;});
function news(args) {
fetch('https://testapi.local/', {
method: 'GET',
}).then(response => {
return response.json();
}).then(function(r) {
var res = r.results;
orderList.length = 0
console.log(JSON.stringify(res));
for (var i = 0; i < res.length; i++) {
var order = {
createdAt: res[i].createdAt,
title: res[i].title,
desc: res[i].desc,
image: res[i].image.url
}
orderList.push(order);
}
},
function(e) {
console.log(e);
});
};
结果是,我的数组仍然按照从早到晚的顺序排序。 非常感谢
试试这个:
var moment = require("moment");
orderList.sort((left: any, right: any) => {
return moment(left.createdAt).isBefore(right.createdAt) ? -1 : 1;
});
您可以使用比较函数对 ObsevableArray 进行排序,但您应该在数组有一些实际值被推入和推入后使用它们:
orderList.sort(function (orderA, orderB) {
var createdAtA = orderA.createdAt; // what is createdAt!? probably normilize it for comparision
var createdAtB = orderB.createdAt;
return (createdAtA < createdAtB ) ? -1 : (createdAtA > createdAtB ) ? 1 : 0;
});