英文:
How to sort numbers from largest to smallest?
问题
I'm trying to sort numbers from largest to smallest, but I didn't manage to do it.
My code sorted numbers with 5
digits perfectly but not the rest.
here is an example:
The result should be:
I don't understand why it doesn't sort everything at once?
template
<ng-container *ngFor="let transfer of transferResponse.PRE">
<ng-container *ngFor="let preptf of transfer.PREPTF">
...
interface
export interface TransferResponse extends ApiResponse {
PRE: Transfer[];
}
export interface Transfer {
DEPO: number;
CLER: string;
INTITULE1: string;
PHONE: string;
PREPTF: {
LABEL: string;
TYPE: string;
QUANTITE: number;
STATUT: number;
NUM: number;
ISIN: string;
STATUT_LIB: string;
REF_RBC: string;
}[];
}
composent
export class OverviewTransfersComponent implements OnInit, OnDestroy {
private unsubscribe$ = new Subject < void > ();
currentAgency: any;
transferResponse: TransferResponse;
constructor(
private service: OverviewTransfersService,
private store: Store,
private modalService: BsModalService,
private router: Router
) {}
ngOnInit(): void {
this.currentAgency = this store.selectSnapshot(AgencyState.currentAgency);
if (this.currentAgency) {
this.OverviewTransfers();
}
}
ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
OverviewTransfers(): void {
console.log('Before the request service.OverviewTransfers()');
this.service.OverviewTransfers().pipe(
takeUntil(this.unsubscribe$)
).subscribe(res => {
console.log('Response service.OverviewTransfers()', res);
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
const transferResponse = res as TransferResponse;
console.log('Before sorting', transferResponse);
transferResponse.PRE.forEach(transfer => {
transfer.PREPTF.sort((a, b) => {
return b.NUM - a.NUM;
});
});
console.log('After sorting', transferResponse);
this.transferResponse = transferResponse;
}
});
}
}
console.log
英文:
I'm trying to sort numbers from largest to smallest, but I didn't manage to do it.
My code sorted numbers with 5
digits perfectly but not the rest.
here is an example:
15366
13070
13069
13068
13067
13066
13065
13064
313883
313880
313878
313876
313874
313872
313870
313869
313868
The result should be:
313883
313880
313878
313876
313874
313872
313870
313869
313868
15366
13070
13069
13068
13067
13066
13065
13064
I don't understand why it doesn't sort everything at once?
template
<ng-container *ngFor="let transfer of transferResponse.PRE">
<ng-container *ngFor="let preptf of transfer.PREPTF">
<tr>
...
<td class="text-center">
<a [routerLink]="['/transfers/customer-transfer-details/' + preptf.NUM]">{{preptf.NUM }} </a>
</td>
...
</tr>
</ng-container>
</ng-container>
interface
export interface TransferResponse extends ApiResponse {
PRE: Transfer[];
}
export interface Transfer {
DEPO: number;
CLER: string;
INTITULE1: string;
PHONE: string;
PREPTF: {
LABEL: string;
TYPE: string;
QUANTITE: number;
STATUT: number;
NUM: number;
ISIN: string;
STATUT_LIB: string;
REF_RBC: string;
}[];
}
composent
export class OverviewTransfersComponent implements OnInit, OnDestroy {
private unsubscribe$ = new Subject < void > ();
currentAgency: any;
transferResponse: TransferResponse;
constructor(
private service: OverviewTransfersService,
private store: Store,
private modalService: BsModalService,
private router: Router
) {}
ngOnInit(): void {
this.currentAgency = this.store.selectSnapshot(AgencyState.currentAgency);
if (this.currentAgency) {
this.OverviewTransfers();
}
}
ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
OverviewTransfers(): void {
console.log('Before the request service.OverviewTransfers()');
this.service.OverviewTransfers().pipe(
takeUntil(this.unsubscribe$)
).subscribe(res => {
console.log('Response service.OverviewTransfers()', res);
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
const transferResponse = res as TransferResponse;
console.log('Before sorting', transferResponse);
transferResponse.PRE.forEach(transfer => {
transfer.PREPTF.sort((a, b) => {
return b.NUM - a.NUM;
});
});
console.log('After sorting', transferResponse);
this.transferResponse = transferResponse;
}
});
}
}
console.log
Response service.OverviewTransfers()
Before sorting service.OverviewTransfers()
After sorting service.OverviewTransfers()
Thank you in advance for your help.
答案1
得分: 1
这不起作用,因为您正在对一个数组内部的数组进行排序。
首先,您有一个TransferResponse
数组,您正在迭代以对子数组PREPTF
进行排序。
如果您想这样做,您需要将所有数字重新分组到一个单一的数组中,然后对它们进行排序。
英文:
It does not work because you are sorting an array inside an array.
First you have an array of TransferResponse
, on which you are iterating to sort the sub array PREPTF
.
If you want to do that, you will need to regroup all the numbers in a single array and then sort them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论