I need to order by ‘Gain’ the list with Ionic 2
fichier json
[
{
Nom: "THANYA WAHDA",
Vict: 1,
plc: 1,
Gain: 11700
},
{
Nom: "ZINJIYAT AL BADR",
Vict: 0,
plc: 2,
Gain: 4900
},
{
Nom: "TEHERAN",
Vict: 0,
plc: 1,
Gain: 2200
},
{
Nom: "RAFII",
Vict: 0,
plc: 2,
Gain: 3200
},
]
you can use pipe => | in *ngFor
just google it you will get your answer definitely
I try but I find no answer
can you show me your code how you write ngFor loop ?
<ion-row *ngFor="let m of members " >
<ion-col width-40> <b> {{m.Nom}} </b> </ion-col>
<ion-col width-30 > {{m.Vict}} </ion-col>
<ion-col width-30 > {{m.Gain}}
</ion-col>
</ion-row>
where is pipe sign ?
just refer this =>
thanks , but this tutorial does not define how sorted by attribute
I need the sort gave me by the gain attribute
i think you are not properly read that code
you have change your desire attribute
return arr.sort((a, b) => {
if (a.likes < b.likes) {
return 1;
}
if (a.likes > b.likes) {
return -1;
}
return 0;
});
return arr.sort((a, b) => {
if (a.gain < b.gain ) {
return 1;
}
if (a.gain > b.gain ) {
return -1;
}
return 0;
});
1 Like
here i make a function to sort the array by key value
just pass array and key to that function
public sortByKey(array, key) {
return array.sort(function (a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 0 : 1));
});
}
calling of this function
var sortedarray = this.sortByKey(this.members, 'gain');
finish!!!
2 Likes
thanks you ,
another question and thank you, I need after the sort of the listing displayed just the first 10
you can use index in ngFor
by the way, you get your sorted array by key or not?
how with index, ?? sorry I understood not
it xill give you index of your item in variable => i
<ion-row *ngFor="let m of members ; let i = index" >
then you have just check i <9
another way you can select only 10 record from database so you don’t have to do any other thing in frontend
don’t forget to mark solution appropriate answer