Angular 角管-排序编号
努力弄清楚如何对数字进行排序。我正在处理一个ngFor循环,我想对我的一些值进行排序。我使用的时间码(电影/电视)数字没有冒号。数字如 01002000 01003000 01000012 我想用管子把这些按asc顺序分类。就我个人而言,我似乎无法让它发挥作用 我已经创建了一个管道,我正在尝试Angular 角管-排序编号,angular,sorting,pipe,Angular,Sorting,Pipe,努力弄清楚如何对数字进行排序。我正在处理一个ngFor循环,我想对我的一些值进行排序。我使用的时间码(电影/电视)数字没有冒号。数字如 01002000 01003000 01000012 我想用管子把这些按asc顺序分类。就我个人而言,我似乎无法让它发挥作用 我已经创建了一个管道,我正在尝试 export class SortTimecodePipe implements PipeTransform { transform(value, direcion): any { ret
export class SortTimecodePipe implements PipeTransform {
transform(value, direcion): any {
return value.sort();
}
}
我也知道sort处理字符串,所以我也尝试过给sort函数一个参数
export class SortTimecodePipe implements PipeTransform {
transform(value, direcion): any {
function compare(a, b) {
return a - b;
}
return value.sort(compare);
}
}
这些选项都不起作用。排序没有进行
你能提供帮助吗
-------编辑-------
返回的数据实际上是一个对象数组,它们如下所示
[
{"timecodeIn": 01000020, "timecodeOut": 01000010, "Reason": "Alt Line", "Line": "Hey there"},
{"timecodeIn": 01000005, "timecodeOut": 01000007, "Reason": "Extra", "Line": "You"}
]
I'm tyring to ngFor this array of objects, sorted by the 'timecodeIn' property.
Thanks for the note on sorting numbers like this over strings, I can see how it would fail now. Also, wondering if sorting in the component is better than the view (pipe)
检查一下。也许您有打字错误或其他问题,但考虑到我们讨论的是字符串:
numbersStr = ['01002000', '01003000', '01000012'];
或数字
numbers = [1002000, 1003000, 1000012];
然后在html中
<div>
<h4> Sorting strings array </h4>
<ul>
<li *ngFor="let number of numbersStr | sortStringsPipe">{{number}}</li>
</ul>
<h4> Sorting number array </h4>
<ul>
<li *ngFor="let number of numbers | sortPipe">
{{number}}</li>
</ul>
<div>
检查一下。也许您有打字错误或其他问题,但考虑到我们讨论的是字符串:
numbersStr = ['01002000', '01003000', '01000012'];
或数字
numbers = [1002000, 1003000, 1000012];
然后在html中
<div>
<h4> Sorting strings array </h4>
<ul>
<li *ngFor="let number of numbersStr | sortStringsPipe">{{number}}</li>
</ul>
<h4> Sorting number array </h4>
<ul>
<li *ngFor="let number of numbers | sortPipe">
{{number}}</li>
</ul>
<div>
你们可以在把数字排好之后使用数字数组。根据我的理解,在你们的情况下不需要使用管道。无论如何,如果您可以在Javascript中添加您的问题,那么如果任何数字以0开头,都将被视为八进制表示。在您的示例中,all(以0开头)将被视为八进制,并将其转换为十进制。因此,为了避免这种情况,请将时间码直接转换为字符串“011002”。然后直接应用sort函数。Mike,考虑一下@PALLAMOLLASAI所说的。此外,如果可能,请共享最终排序结果。要获得最快的解决方案,请尝试添加stackblitz.com演示代码制作stackblize示例以快速获得解决方案;你们可以在把数字排好之后使用数字数组。根据我的理解,在你们的情况下不需要使用管道。无论如何,如果您可以在Javascript中添加您的问题,那么如果任何数字以0开头,都将被视为八进制表示。在您的示例中,all(以0开头)将被视为八进制,并将其转换为十进制。因此,为了避免这种情况,请将时间码直接转换为字符串“011002”。然后直接应用sort函数。Mike,考虑一下@PALLAMOLLASAI所说的。此外,如果可能,请共享最终排序结果。要获得最快的解决方案,请尝试添加stackblitz.com演示代码制作stackblize示例以快速获得解决方案;