First, create a model class that represents your incoming object as shown below -
export class Countries {
country: string;
countryCode: string;
}
Then initialize it as an array of objects -
countriesModel : Countries [] = [];
Convert your JSON to a model by parsing it -
this.countryModel = JSON.parse(your_country-object);
After that call the sortCountries function and pass the property in the object on which you want to sort -
this.sortCountries(p => p.country, 'ASC');
Finally, your sort functions body should look something like this -
sortCountries<T>(countryName: (c: countriesModel) => T, order: 'ASC' | 'DESC'): void {
this.countriesModel.sort((a, b) => {
if (countryName(a) < countryName(b)) {
return -1;`enter code here`
} else if (countryName(a) > countryName(b)) {
return 1;
} else {
return 0;
}
});
if (order === 'DESC') {
this.countriesModel.reverse();
}
}