Skip to content

Instantly share code, notes, and snippets.

@mdmoin7
Last active July 3, 2022 07:18
Show Gist options
  • Select an option

  • Save mdmoin7/73eea358d03194d6fc17c65f379df0e3 to your computer and use it in GitHub Desktop.

Select an option

Save mdmoin7/73eea358d03194d6fc17c65f379df0e3 to your computer and use it in GitHub Desktop.
/*
*ngFor="let c of oneDimArray | sortBy:'asc'"
*ngFor="let c of arrayOfObjects | sortBy:'asc':'propertyName'"
*/
import { Pipe, PipeTransform } from '@angular/core';
import { orderBy } from 'lodash';
@Pipe({ name: 'sortBy' })
export class SortByPipe implements PipeTransform {
transform(value: any[], order = '', column: string = ''): any[] {
if (!value || order === '' || !order) { return value; } // no array
if (value.length <= 1) { return value; } // array with only one item
if (!column || column === '') {
if(order==='asc'){return value.sort()}
else{return value.sort().reverse();}
} // sort 1d array
return orderBy(value, [column], [order]);
}
}
@Virkom

Virkom commented Jan 27, 2020

Copy link
Copy Markdown

There is a mistake in code. Symbol "_" not found. Need to be like this:

import { sortBy, orderBy } from 'lodash';

.....

return orderBy(value, [column], [order]);

@mdmoin7

mdmoin7 commented Jan 27, 2020

Copy link
Copy Markdown
Author

Thanks for the mention, had designed it way back forgot to update it to newer annotations completely

@Dschuli

Dschuli commented Mar 6, 2020

Copy link
Copy Markdown

Hi - I get the following error in vscode - line 15 ... [order]. Any idea what to do?

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string[]' is not assignable to parameter of type 'Many<boolean | "asc" | "desc">'.
      Type 'string[]' is not assignable to type 'readonly (boolean | "asc" | "desc")[]'.
        Type 'string' is not assignable to type 'boolean | "asc" | "desc"'.ts(2769)
collection.d.ts(1339, 9): The last overload is declared here.

@mdmoin7

mdmoin7 commented Mar 6, 2020

Copy link
Copy Markdown
Author

Have you referred the comment for usage, because both column and order accept only string if it is an array of objects. If it's a single dimensional array only order is required

@Dschuli

Dschuli commented Mar 7, 2020

Copy link
Copy Markdown

Looks like a type issue which I have to address by myself. Lodash orderBy only accepts boolean | "asc" | "desc". Thx anyhow.

@ReeceXmen

Copy link
Copy Markdown

Hi - I get the following error in vscode - line 15 ... [order]. Any idea what to do?

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string[]' is not assignable to parameter of type 'Many<boolean | "asc" | "desc">'.
      Type 'string[]' is not assignable to type 'readonly (boolean | "asc" | "desc")[]'.
        Type 'string' is not assignable to type 'boolean | "asc" | "desc"'.ts(2769)
collection.d.ts(1339, 9): The last overload is declared here.

I'm getting this too !
Has this been figured out ? I can't seem to figure out why it is giving me this error.
~ Thanks

@mdmoin7

mdmoin7 commented Jul 3, 2022

Copy link
Copy Markdown
Author

The above issue started to happen due to lodash changes, you can change the type for the argument order:any

@ReeceXmen

Copy link
Copy Markdown

The above issue started to happen due to lodash changes, you can change the type for the argument order:any

Thank You !!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment