Let's say I have an array of strings:
this.data = [ 'cupcake', 'donut', 'eclair', 'froyo', 'gingerbread', 'icecream', 'lollipop', 'marshmallow', 'nougat', 'oreo' ]
I can sort these alphabetically using the pipe:
@Pipe({
name: 'alphabeticPipe'
})
export class AlphabeticPipe implements PipeTransform {
transform(data: any[], searchTerm: string): any[] {
data.sort((a: any, b: any) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return data;
}
}
My question is how could I sort these with a custom alphabetical order, for example I want "e" to show first, then "g", then "m" etc. So the order will look like: eclair, gingerbread, marshmallow and the rest can follow alphabetical order or anything else specified?
Thanks for the help!