Michael
Michael

Reputation: 5048

Using Javascript .sort function to sort strings containing numbers

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

Upvotes: 0

Views: 61

Answers (2)

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

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;}

Upvotes: 1

Nilanka Manoj
Nilanka Manoj

Reputation: 3728

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

Upvotes: 1

Related Questions