如何将数字从大到小排序?

huangapple go评论120阅读模式
英文:

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">

...

<a [routerLink]="['/transfers/customer-transfer-details/' + preptf.NUM]">{{preptf.NUM }}

...


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

&lt;ng-container *ngFor=&quot;let transfer of transferResponse.PRE&quot;&gt;
   &lt;ng-container *ngFor=&quot;let preptf of transfer.PREPTF&quot;&gt;
      &lt;tr&gt;
         ...
         &lt;td class=&quot;text-center&quot;&gt;
            &lt;a [routerLink]=&quot;[&#39;/transfers/customer-transfer-details/&#39; + preptf.NUM]&quot;&gt;{{preptf.NUM }} &lt;/a&gt;
         &lt;/td&gt;
         ...
      &lt;/tr&gt;
   &lt;/ng-container&gt;
&lt;/ng-container&gt;

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 &lt; void &gt; ();
   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(&#39;Before the request service.OverviewTransfers()&#39;);
      this.service.OverviewTransfers().pipe(
         takeUntil(this.unsubscribe$)
      ).subscribe(res =&gt; {
         console.log(&#39;Response service.OverviewTransfers()&#39;, res);
         if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
            const transferResponse = res as TransferResponse;
            console.log(&#39;Before sorting&#39;, transferResponse);
            transferResponse.PRE.forEach(transfer =&gt; {
               transfer.PREPTF.sort((a, b) =&gt; {
                  return b.NUM - a.NUM;
               });
            });
            console.log(&#39;After sorting&#39;, 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.

huangapple
  • 本文由 发表于 2023年3月15日 17:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定