Ionic Order by List


#1

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
},
]

#2

you can use pipe => | in *ngFor
just google it you will get your answer definitely


#3

I try but I find no answer


#4

can you show me your code how you write ngFor loop ?


#5
   <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>

#6

where is pipe sign ?
just refer this =>


#7

thanks , but this tutorial does not define how sorted by attribute
I need the sort gave me by the gain attribute


#8

i think you are not properly read that code


#9

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;
    });

#10

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!!!:wink::wink::wink:


#11

thanks you ,
another question and thank you, I need after the sort of the listing displayed just the first 10


#12

you can use index in ngFor


#13

by the way, you get your sorted array by key or not?


#14

how with index, ?? sorry I understood not


#15

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


#16

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