SortPipe passing parameter

Published
import { Pipe, PipeTransform } from '@angular/core';
import { courses } from '../interface/courses';

@Pipe({
  name: 'sort'
})
export class SortPipe implements PipeTransform {
  transform(value: Array<courses>, args: any[]): Array<courses> {
    const sortField = args[0];
      const sortDirection = args[1];
      let multiplier = 1;

       if (sortDirection === 'desc') {
         multiplier = -1;
        }
        
      value.sort((a: any, b: any) => {
  
       if (a[sortField] < b[sortField]) {
            return -1 *multiplier ;
       } else if (a[sortField]> b[sortField]) {
         return 1 *multiplier;
       } else {
         return 0;
        }       
    });

    return value;
  }  
}

this is the sort pipe iam using for my application, I need to pass sortFiled category as parameter from the below interface list

export interface courses {
  courseId?: number;
  trainingPlatform: string;
  courseName: string;
  platformName: string;
  courseUrl: string;
  learningHours: number;
  courseAssignment: {
    assignmentId: number;
    userId: string;
    startDate: Date;
    endDate: Date;
    category: string;
    trainingType: string;
    courseId: number;
    courseStatus: string;
    completedDateTime: Date;
  };
}

I tried to pass like this courseAssignment.category and also course.courseAssignment.category both are not working need some help

Source: Angular Questions

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq