angular - 在 Angular/Typescript 中用整数和字母按字母顺序对数组进行排序

标签 angular typescript firebase ionic2 angularfire2

我打算做什么

我尝试像这样对数组进行排序...

  • 1
  • 2
  • 2(a)
  • 2(b)
  • 2(b) #AsimpleName
  • 2(b) #NameWithN
  • 3
  • 4
  • 4(a)
  • ...

...在 Angular2 中。

我的当前代码

组件

this.streetDetailRef = this.afDatabase.list('data/users/' + this.currentUserID + '/territory/' + this.sStreet.parentKey + '/' + this.sStreet.key + '/houseNumbers/');
this.streetDetailData = this.streetDetailRef.snapshotChanges().map(changes => {
  return changes.map(c => ({ key: c.payload.key, ...c.payload.val() })).sort();
});

我眼中的循环

<ion-item-sliding *ngFor="let house of streetDetailData | async | orderBy: 'number':false" #slidingItem>
<strong>{{ house.number }}</strong> ...

'number' 在这种情况下是没有字母的干净数字。我将这封信存储在一个单独的 firebase 条目中。但如果需要,肯定可以将它们存储在同一个地方。

附加信息:我正在使用 VadimDez 的 ngx-orderBy-pipe:https://github.com/VadimDez/ngx-order-pipe

当前结果

enter image description here

最佳答案

这个函数应该可以解决问题:

function sortData(array: Array<number | string>): Array<number | string> {
    return array.sort((a, b) => a < b ? -1 : 1);
}

这里是使用示例:

const sorted = sortData(['4(a)', 4, 3, '2(b) #NameWithN', '2(b) #AsimpleName']); 
sorted // [ '2(b) #AsimpleName', '2(b) #NameWithN', 3, 4, '4(a)' ]

关于angular - 在 Angular/Typescript 中用整数和字母按字母顺序对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743466/

相关文章:

json - Angular HttpClient 错误处理困难

javascript - 我需要从 Angular 2 中的其他组件获取公共(public)方法

angular - 我可以跨应用重用组件吗?

angular - AWS Cognito : Methods to Get a List of users?

android - 如何让 GTM 使用 Firebase 运行?

Angular2 DI 与 ES2016 装饰器?

Angular 导入成本

javascript - 获取跨源请求的响应自定义 header

firebase - 我的 Firebase 实时数据库安全规则有问题

javascript - 无法检索 Firebase 数据的键