Angular 按字母顺序排列的角度对象[]

Angular 按字母顺序排列的角度对象[],angular,typescript,sorting,multidimensional-array,lodash,Angular,Typescript,Sorting,Multidimensional Array,Lodash,我正在研究Angular 7,我需要对多维数组进行排序。我有一个框,我的用户可以在其中选择国籍。我有一个Country[]界面,里面有两个字段CountryCode和CountryName 我想按CountryName排序,我的用户将能够按照正确的字母顺序选择国籍。我发现Pipes和Lodash librairy可以帮助我创建自定义管道 这是从我的数据库中获取我的CountryCode和CountryName的实际代码 private getCountryData() { this.findAl

我正在研究Angular 7,我需要对多维数组进行排序。我有一个框,我的用户可以在其中选择国籍。我有一个
Country[]
界面,里面有两个字段
CountryCode
CountryName

我想按
CountryName
排序,我的用户将能够按照正确的字母顺序选择国籍。我发现
Pipes
Lodash librairy
可以帮助我创建自定义管道

这是从我的数据库中获取我的
CountryCode
CountryName
的实际代码

private getCountryData() {
this.findAllCountries().subscribe((countries: Country[]) => {
this.countryList = country;
});
现在我试着用Lodash

transform(value: Country[], by: string, direction = "asc" | "desc"): Country[] {
    this.countryList = _.orderBy(this.countryList, ['string', 'countryLabel'], ['asc', 'desc']);
    return this.countryList;
  }

谢谢大家!

您不需要
lodash
,可以使用JavaScript进行排序,如下所示

var国家=[
{名称:'India',代码:'IN'},
{名称:'法国',代码:'法国'},
{名称:'Australia',代码:'AU'},
];
var sorted=countries.sort((a,b)=>a.name.localeCompare(b.name));

控制台日志(已排序)您不需要
lodash
,可以使用JavaScript进行排序,如下所示

var国家=[
{名称:'India',代码:'IN'},
{名称:'法国',代码:'法国'},
{名称:'Australia',代码:'AU'},
];
var sorted=countries.sort((a,b)=>a.name.localeCompare(b.name));
控制台日志(已排序)
让asc=“asc”;
让desc=“desc”;
让direction=“DESC”;
let国家:国家[]=[{
国家代码:“IN”,
国名:“印度”
},
{
国家代码:“美国”,
国名:“美国”
},
{
国家代码:“英国”,
国名:“英格兰”
}
];
如果(方向=“ASC”){
countries.sort((a,b)=>(a.CountryName>b.CountryName)?1:-1)
}否则{
countries.sort((a,b)=>(a.CountryName>b.CountryName)?-1:1)
}
countries.forEach(项目=>{
console.log(item.CountryName);
});
让asc=“asc”;
让desc=“desc”;
让direction=“DESC”;
let国家:国家[]=[{
国家代码:“IN”,
国名:“印度”
},
{
国家代码:“美国”,
国名:“美国”
},
{
国家代码:“英国”,
国名:“英格兰”
}
];
如果(方向=“ASC”){
countries.sort((a,b)=>(a.CountryName>b.CountryName)?1:-1)
}否则{
countries.sort((a,b)=>(a.CountryName>b.CountryName)?-1:1)
}
countries.forEach(项目=>{
console.log(item.CountryName);

});试试这个,如果你还有任何问题请告诉我。试试这个,如果你还有任何问题请告诉我。