Skip to content
Not available in DartThis function is not part of the Dart package. The documentation below is for JavaScript and Python.

sortByObjectKey

Sort array values by a specific key value in an array containing multiple objects. It does not affect the order or value of elements within an object.

If the numerically option is trueTrue, when sorting an array consisting of strings, it sorts first by the numbers contained in the strings, not by their names. That ordering is the one sortNumeric describes.

Parameters

NameTypeRequiredDefault
arrayany[]list[Any]
keystringstr
descendingbooleanboolfalseFalse
numericallybooleanboolfalseFalse

Returns

any[]

list[Any]

Examples

javascript
const obj = [
	{
		aa: 1,
		bb: 'aaa',
		cc: 'hi1'
	},
	{
		aa: 4,
		bb: 'ccc',
		cc: 'hi10'
	},
	{
		aa: 2,
		bb: 'ddd',
		cc: 'hi2'
	},
	{
		aa: 3,
		bb: 'bbb',
		cc: 'hi11'
	}
];

sortByObjectKey(obj, 'aa');

/*
[
	{
		aa: 1,
		bb: 'aaa',
		cc: 'hi1'
	},
	{
		aa: 2,
		bb: 'ddd',
		cc: 'hi2'
	},
	{
		aa: 3,
		bb: 'bbb',
		cc: 'hi11'
	},
	{
		aa: 4,
		bb: 'ccc',
		cc: 'hi10'
	}
]
*/
python
obj = [
	{
		'aa': 1,
		'bb': 'aaa',
		'cc': 'hi1'
	},
	{
		'aa': 4,
		'bb': 'ccc',
		'cc': 'hi10'
	},
	{
		'aa': 2,
		'bb': 'ddd',
		'cc': 'hi2'
	},
	{
		'aa': 3,
		'bb': 'bbb',
		'cc': 'hi11'
	}
]

sortByObjectKey(obj, 'aa')

# [
# 	{
# 		'aa': 1,
# 		'bb': 'aaa',
# 		'cc': 'hi1'
# 	},
# 	{
# 		'aa': 2,
# 		'bb': 'ddd',
# 		'cc': 'hi2'
# 	},
# 	{
# 		'aa': 3,
# 		'bb': 'bbb',
# 		'cc': 'hi11'
# 	},
# 	{
# 		'aa': 4,
# 		'bb': 'ccc',
# 		'cc': 'hi10'
# 	}
# ]

Released under the MIT License