angular form control and formarray sort using perticular key asc and desc
1). FormArray sorting
sortFormArray(array: any, args, type = 'asc') {
if (array !== undefined) {
return array.controls.sort((a: any, b: any) => {
const aValue = a.controls[args].value;
const bValue = b.controls[args].value;
let condition1 = aValue > bValue;
let condition2 = aValue < bValue;
if (type === 'asc') {
condition1 = aValue < bValue;
condition2 = aValue > bValue;
}
if (condition1) {
return -1;
} else if (condition2) {
return 1;
} else {
return 0;
}
});
}
return array;
}
Example:
const Address= FormArray();
sortFormArray(Address, 'CountryID','asc');
2).sort list data
getsort(List?) {
if (List) {
return List.sort((a, b) => (a > b) ? 1 : -1);
} else { return null; }
}
Example:
getsort(this.items);
getsort(this.items);
3).sorting using lodash
getsortNew(List?,key:[],type:[]) {
if (List) {
return lodash.sortBy(List, key,type);
} else { return null; }
}
Example:
getsortNew(this.items,['CountryID'],[asc]);
getsortNew(this.items,['CountryID'],[desc]);
No comments:
Post a Comment
Thank You For Your Great Contribution