Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0: {playerName: "Imran Tahir", country: "Dolphins", playerCreditPoint: "1.50"}
- 1: {playerName: "xyx", country: "Dolphins", playerCreditPoint: "6.50"}
- 2: {playerName: "abc", country: "Dolphins", playerCreditPoint: "4.50"}
- 3: {playerName: "def", country: "Dolphins", playerCreditPoint: "11.50"}
- 4: {playerName: "mno", country: "Dolphins", playerCreditPoint: "10.50"}
- 5: {playerName: "pqr", country: "Dolphins", playerCreditPoint: "9.50"}
- <div class="all_players" *ngFor="let player of players | orderBy :player; let pIndex = index">
- import {Pipe, PipeTransform} from '@angular/core';
- @Pipe({
- name: 'orderBy'
- })
- export class OrderBy implements PipeTransform {
- transform(array, orderBy) {
- // descending
- return Array.from(array).sort((item1: any, item2: any) =>
- this.comparator(item2[orderBy], item1[orderBy])
- );
- }
- comparator(a: any, b: any): number {
- if (parseFloat(a) < parseFloat(b)) {
- return -1;
- }
- if (parseFloat(a) > parseFloat(b)) {
- return 1;
- }
- return 0;
- }
- }
- const data = [
- { playerName: "Imran Tahir", country: "Dolphins", playerCreditPoint: 1.5 },
- { playerName: "xyx", country: "Dolphins", playerCreditPoint: 6.5 },
- { playerName: "abc", country: "Dolphins", playerCreditPoint: 4.5 },
- { playerName: "def", country: "Dolphins", playerCreditPoint: 11.5 },
- { playerName: "mno", country: "Dolphins", playerCreditPoint: 10.5 },
- { playerName: "pqr", country: "Dolphins", playerCreditPoint: 9.5 }
- ];
- function orderBy(arr) {
- return arr.sort((item1: any, item2: any) => {
- return comparator(item2, item1);
- });
- }
- function comparator(a: any, b: any) {
- return a.playerCreditPoint - b.playerCreditPoint;
- }
- console.log(orderBy(data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement