2 回答
TA贡献1735条经验 获得超5个赞
像这样的东西应该工作:
// Function for asccending order
if (sort === 'alphaAsc') {
filteredLocations = filteredLocations.sort((a, b) =>
a.addressLine1 > b.addressLine1 ? 1 : a.addressLine1 < b.addressLine1 ? -1 : 0
);
}
// Function for descending sort
if (sort === 'alphaDesc') {
filteredLocations = filteredLocations.sort((a, b) =>
b.addressLine1 > a.addressLine1 ? -1 : a.addressLine1 < b.addressLine1 ? 1 : 0
);
}
TA贡献1777条经验 获得超3个赞
对于每个过滤器函数,如果对象 b 应位于对象 a 之前,则返回 1,如果对象 a 应位于对象 b 之前,则返回 -1,如果对象的顺序正确,则返回 0。
if (sort === "alphaAsc") {
filteredLocations = filteredLocations.sort((a, b) => {
if (a.addressLine1 < b.addressLine1) return -1;
if (a.addressLine1 > b.addressLine1) return 1;
return 0;
});
};
if (sort === "alphaDesc") {
filteredLocations = filteredLocations.sort((a, b) => {
if (a.addressLine1 > b.addressLine1) return -1;
if (a.addressLine1 < b.addressLine1) return 1;
return 0;
});
};
添加回答
举报