0

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)?

9
  • The subtraction operator works with numbers, not alpha strings. Commented Apr 26, 2020 at 13:09
  • .sort((a, b) => a.code_name > b.code_name ? 1 : -1). Use this. Commented Apr 26, 2020 at 13:10
  • Try "SNOMED" - "UMLS" in your browser console. Commented Apr 26, 2020 at 13:10
  • 1
    a.code_name.localeCompare(b.code_name) would be better. Commented Apr 26, 2020 at 14:08
  • 1
    @Michael also note that the root question with the performance issue is 7 years old, right about the time that .localeCompare() became available. Commented Apr 26, 2020 at 15:28

2 Answers 2

1

sort accepts the comparator function for an ordered pair of elements. eg:

for elements e1,e2 :

if e1 > e2 compartor = 1 
if e1 < e2 compartor = -1 
if e1 = e2 compartor = 0 

cor your case :

const comparator = (e1,e2) => {
    if(e1.code_name>e2.code_name)return 1
    if(e1.code_name<e2.code_name)return -1
    if(e1.code_name>e2.code_name)return 0
}

test forward :

let input = [{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"2BPRECISE","code_system_id":"4567"},
{"code_name":"UMLS","code_system_id":"7894"}]

const comparator = (e1,e2) => {
    if(e1.code_name>e2.code_name)return 1
    if(e1.code_name<e2.code_name)return -1
    if(e1.code_name>e2.code_name)return 0
}

console.log(input.sort(comparator))

test revers :

let input = [{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"2BPRECISE","code_system_id":"4567"},
{"code_name":"UMLS","code_system_id":"7894"}]

const comparator = (e1,e2) => {
    if(e1.code_name>e2.code_name)return -1
    if(e1.code_name<e2.code_name)return 1
    if(e1.code_name>e2.code_name)return 0
}

console.log(input.sort(comparator))

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the third if; if one is not greater than the other, the only other possibility is that they're equal.
yeah, it can be removed. I post this just to explain how the comparator works.
1

You can try this:

const data = [{"code_name":"SNOMED","code_system_id":"1234"},{"code_name":"2BPRECISE","code_system_id":"4567"},
{"code_name":"UMLS","code_system_id":"7894"},
{"code_name":"SNOMED","code_system_id":"1234"}];


const compare = (dirn) => {
	if (dirn === 'asc') {
	    return (a, b) => {
		if (a.code_name === b.code_name) return 0;
		return a.code_name > b.code_name ? 1 : -1;
	    }
	} else if (dirn === 'desc') {
	    return (a, b) => {
		if (a.code_name === b.code_name) return 0;
		return a.code_name > b.code_name ? -1 : 1;
	    }
	}
}

console.log('asc', data.sort(compare('asc')));
console.log('desc', data.sort(compare('desc')));
.as-console-wrapper {min-height: 100%!important; top: 0;}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.