Angular7-排序按字母顺序选择下拉选项



我有一个商品项目列表,其中我必须按字母顺序对所有IDM项目进行排序,在该DM项目下方按字母顺序进行排序,现在编写我的代码显示为错误:顺序:但我想显示为正确第一个IDM和下一个DM

**WRONG:**
DM-Air
DM-BELL
DM-CEL
IDM-AIR
IDM-BELL
IDM-CELL
**CORRECT**
IDM-AIR
IDM-BELL
IDM-CELL
DM-Air
DM-BELL
DM-CEL

我的代码是

this.supplier.getCommoditiesFamilyList().subscribe(
(data: any) => {
this.commoditiesFamilyList = data;
this.commoditiesFamilyList.sort(this.commoditySortByName);
},
this.supplier.getCommoditiesFamilyList(id).subscribe(
data => {
this.commoditiesFamilyList = data;
this.commoditiesFamilyList.sort(this.commoditySortByName);
},
[![this.subscriberLevelId = id;
this.supplier.getCommoditiesFamilyList(id).subscribe(
data => {
this.commoditiesFamilyList = data;
this.commoditiesFamilyList.sort(this.commoditySortByName);
},][1]][1]
public commoditySortByName(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
}

您只需在commoditySortByName函数中切换1-1

我已经为您写了这篇文章,但不知道在分离之前如何对零件进行排序。我的猜测是颠倒的顺序?

this.commoditiesFamilyList = this.commoditiesFamilyList
.map(item => {
var splitted: string[] = item.split('-');
return {'pre': splitted[0], 'post': splitted[1]};
})
.sort(this.commoditySortByPre)
.sort(this.commoditySortByPost)
.map(item => item.pre + '-' + item.post);
public commoditySortByPre(a, b) {
if (a.pre < b.pre) {
return 1;
}
if (a.pre > b.pre) {
return -1;
}
return 0;
}
public commoditySortByPost(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}

最新更新