2 回答

TA貢獻1803條經驗 獲得超6個贊
在radio值改變事件的回調函數中調用該函數。
function sortCitoyens(citoyens, isAsc) {
if(isAsc){
citoyens.sort((a,b)=>a.id-b.id)
}else{
citoyens.sort((a,b)=>b.id-a.id)
}
}

TA貢獻1851條經驗 獲得超3個贊
有人發(fā)布了答案,然后將其刪除,但我有時間這樣做并且它有效,所以謝謝大家的幫助:)
<section class="col-12">
<h2>Sélection Par Nom:</h2>
<select (change)="surSelection($event.target.value)">
<option value="" selected disabled hidden>Choisissez:</option>
<option *ngFor="let cito of citoyens.sort()" [value]="cito.id">{{cito.nom}} : {{cito.id}}</option>
</select>
<button (click)="activerSelectionNAS()"> Trier Par NAS </button>
<section>
<input type="radio" id="asc" name="sort" (click)="sort('asc')">
<label for="asc">Ascendant</label><br>
<input type="radio" id="desc" name="sort" (click)="sort('desc')" >
<label for="desc">Descendant</label>
</section>
</section>
sort(type: string) {
const sortList = (a: any, b: any) => a.nom.toLowerCase().localeCompare(b.nom.toLowerCase());
this.citoyens = type === "asc"
? this.citoyens.sort((a: any, b: any) => sortList(a, b))
: this.citoyens.sort((a: any, b: any) => sortList(b, a));
}
添加回答
舉報