我想将来自函数的数据数组传递给排序。例如:
const DEFAULT_COMPETITORS = [ 'Seamless/Grubhub', 'test'];
DEFAULT_COMPETITORS.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});上面的工作正常。但是我想要来自function的数据,而不是DEFAULT_COMPETITORS const。我希望如下所示:
我的数据是getAllCompetitors格式的,而不是const格式。
function getAllCompetitors() {
$.ajax({
url: '/salescrm/getTopCompetitorsList',
type: 'POST',
success: function(data) {
console.log('getAllCompetitors data: ',data);
response(data);
},
error: function(data) {
console.log('data error: ',data);
}
});
}
getAllCompetitors.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}); 希望你们有..。有人能帮帮我吗?
提前谢谢你,
发布于 2018-08-08 14:55:22
我希望这能行得通
function getAllCompetitors() {
return $.ajax({
url: '/salescrm/getTopCompetitorsList',
type: 'POST',
});
}
getAllCompetitors()
.then(res => {
// you can sort the data
let sortedData = res.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
console.log("sortedData once ajax call made the success",sortedData)
})
.fail(err= > console.log(err))
发布于 2018-08-08 15:31:36
下面是一个简单的例子:
在任何数组中,您都可以使用函数.sort()对默认排序规则进行排序,或者使用带有函数的.sort来使用您在方法中提供的自定义排序规则
默认排序:
var items = ['baa', 'aaa', 'aba',"Caa"];
var sortedItems = items.sort();
console.log(sortedItems);
自定义排序:
var items = ['baa', 'aaa', 'aba','Caa'];
var sortedItems = items.sort(function(item1,item2){
// Locale text case insensitive compare
return item1.toLowerCase().localeCompare(item2.toLowerCase());
});
console.log(sortedItems);
https://stackoverflow.com/questions/51740231
复制相似问题