I have following Array of Objects containing strings:
[{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"2BPRECISE","code_system_id":"4567"},
{"code_name":"UMLS","code_system_id":"7894"}]
The strings need to be sorted in alphabetical order, the following function was used:
.sort((a, b) => a.code_name - b.code_name)
Expectation:
[{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"UMLS","code_system_id":"7894"},
{"code_name":"2BPRECISE","code_system_id":"4567"}]
OR
[{"code_name":"2BPRECISE","code_system_id":"4567"},{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"UMLS","code_system_id":"7894"}]
Actual Results:
[{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"2BPRECISE","code_system_id":"4567"},
{"code_name":"UMLS","code_system_id":"7894"}]
My understanding is that the .sort function sorts based on utf-16 although unclear how then to get my expected result.
How can I get my expected result (either one)?
.sort((a, b) => a.code_name > b.code_name ? 1 : -1). Use this."SNOMED" - "UMLS"in your browser console.a.code_name.localeCompare(b.code_name)would be better..localeCompare()became available.