Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. 0: {playerName: "Imran Tahir", country: "Dolphins", playerCreditPoint: "1.50"}
  2. 1: {playerName: "xyx", country: "Dolphins", playerCreditPoint: "6.50"}
  3. 2: {playerName: "abc", country: "Dolphins", playerCreditPoint: "4.50"}
  4. 3: {playerName: "def", country: "Dolphins", playerCreditPoint: "11.50"}
  5. 4: {playerName: "mno", country: "Dolphins", playerCreditPoint: "10.50"}
  6. 5: {playerName: "pqr", country: "Dolphins", playerCreditPoint: "9.50"}
  7.  
  8. <div class="all_players" *ngFor="let player of players | orderBy :player; let pIndex = index">
  9.  
  10. import {Pipe, PipeTransform} from '@angular/core';
  11.  
  12. @Pipe({
  13. name: 'orderBy'
  14. })
  15. export class OrderBy implements PipeTransform {
  16. transform(array, orderBy) {
  17. // descending
  18. return Array.from(array).sort((item1: any, item2: any) =>
  19. this.comparator(item2[orderBy], item1[orderBy])
  20. );
  21. }
  22.  
  23. comparator(a: any, b: any): number {
  24. if (parseFloat(a) < parseFloat(b)) {
  25. return -1;
  26. }
  27. if (parseFloat(a) > parseFloat(b)) {
  28. return 1;
  29. }
  30.  
  31. return 0;
  32. }
  33. }
  34.  
  35. const data = [
  36. { playerName: "Imran Tahir", country: "Dolphins", playerCreditPoint: 1.5 },
  37. { playerName: "xyx", country: "Dolphins", playerCreditPoint: 6.5 },
  38. { playerName: "abc", country: "Dolphins", playerCreditPoint: 4.5 },
  39. { playerName: "def", country: "Dolphins", playerCreditPoint: 11.5 },
  40. { playerName: "mno", country: "Dolphins", playerCreditPoint: 10.5 },
  41. { playerName: "pqr", country: "Dolphins", playerCreditPoint: 9.5 }
  42. ];
  43.  
  44. function orderBy(arr) {
  45. return arr.sort((item1: any, item2: any) => {
  46. return comparator(item2, item1);
  47. });
  48. }
  49.  
  50. function comparator(a: any, b: any) {
  51. return a.playerCreditPoint - b.playerCreditPoint;
  52. }
  53.  
  54. console.log(orderBy(data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement