在javascript/vue中按名称对国家进行排序
我正在使用图书馆: 我无法在select组件中按名称对国家进行排序 谢谢大家! 从 我想你应该用排序而不是过滤器在javascript/vue中按名称对国家进行排序,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我正在使用图书馆: 我无法在select组件中按名称对国家进行排序 谢谢大家! 从 我想你应该用排序而不是过滤器 var items = [ { name: "Edward", value: 21 }, { name: "Sharpe", value: 37 }, { name: "And", value: 45 }, { name: "The", value: -12 }, { name: "Magnetic", value: 13 }, { name: "Zeros"
var items = [
{ name: "Edward", value: 21 },
{ name: "Sharpe", value: 37 },
{ name: "And", value: 45 },
{ name: "The", value: -12 },
{ name: "Magnetic", value: 13 },
{ name: "Zeros", value: 37 }
];
items.sort(function (a, b) {
return a.value - b.value;
});
在您的情况下,这可能会起作用:
loadCountries() {
return require("country-data")
.countries.all.sort((a, b) => a.name - b.name)
.map(country => ({
label: country.name,
value: country.alpha3
}));
}
你也可以使用
您可以尝试使用方法而不是筛选器:
<template>
<div></div>
</template>
<script>
let countries = require("country-data").countries;
export default {
mounted() {
let sortedCountries = countries.all
.sort((c1, c2) => {
if (c1.name < c2.name) return -1;
if (c1.name > c2.name) return 1;
return 0;
})
.map(country => {
return {
name: country.name,
alpha3: country.alpha3
};
});
console.log("sortedCountries: ", sortedCountries);
}
};
</script>
<style scoped>
</style>
let countries=需要(“国家数据”)。国家;
导出默认值{
安装的(){
让sortedCountries=countries.all
.排序((c1,c2)=>{
if(c1.namec2.name)返回1;
返回0;
})
.map(国家=>{
返回{
名称:country.name,
字母3:国家。字母3
};
});
console.log(“sortedCountries:,sortedCountries”);
}
};
您将看到按国家名称字母顺序排列的输出,如下所示:
已更新:
您最好使用locale和options参数,这些参数允许应用程序指定应该使用其排序顺序的语言,并自定义函数的行为
<script>
let countries = require("country-data").countries;
export default {
mounted() {
let sortedCountries = countries.all
.sort((c1, c2) => c1.name.localeCompare(c2.name))
.map(country => {
return {
name: country.name,
alpha3: country.alpha3
};
});
console.log("sortedCountries: ", sortedCountries);
}
};
</script>
let countries=需要(“国家数据”)。国家;
导出默认值{
安装的(){
让sortedCountries=countries.all
.sort((c1,c2)=>c1.name.localeCompare(c2.name))
.map(国家=>{
返回{
名称:country.name,
字母3:国家。字母3
};
});
console.log(“sortedCountries:,sortedCountries”);
}
};
您将得到一个稍微不同的列表:
@tomatito供您参考,(a,b)=>a.name-b.name不会像另一个答案中那样作为sort()的比较函数。供您参考,(a,b)=>a.name-b.name不会作为sort()的比较函数
<template>
<div></div>
</template>
<script>
let countries = require("country-data").countries;
export default {
mounted() {
let sortedCountries = countries.all
.sort((c1, c2) => {
if (c1.name < c2.name) return -1;
if (c1.name > c2.name) return 1;
return 0;
})
.map(country => {
return {
name: country.name,
alpha3: country.alpha3
};
});
console.log("sortedCountries: ", sortedCountries);
}
};
</script>
<style scoped>
</style>
<script>
let countries = require("country-data").countries;
export default {
mounted() {
let sortedCountries = countries.all
.sort((c1, c2) => c1.name.localeCompare(c2.name))
.map(country => {
return {
name: country.name,
alpha3: country.alpha3
};
});
console.log("sortedCountries: ", sortedCountries);
}
};
</script>