I need to sort labels in angular. I am doing it from the component.ts file. The sorting works fine. It tasks the key value of the label for sorting. But my application is in two languages. so sorting it using key doesn't work when I switch to a different language. Is there a way I can sort it in any language
getlabel(Id: string): Observable<Dto[]> {
return this.httpClient
.get(
url
)
.pipe(
map((data: any) => {
data.sort((a, b) => {
return a.label > b.label ? 1 : -1
})
return data
})
) as Observable
}
Use localeCompare function:
.pipe(
map((data: any) => {
return data.sort((a, b) => {
return a.label.localeCompare(b.label, 'he');
})
})
) as Observable
'he' is for Hebrew language, change it to your language code