Skip to content

Instantly share code, notes, and snippets.

@matheusmurta
Last active May 19, 2020 16:57
Show Gist options
  • Save matheusmurta/c759c3c48af45b905bb7fe3572ac4df3 to your computer and use it in GitHub Desktop.
Save matheusmurta/c759c3c48af45b905bb7fe3572ac4df3 to your computer and use it in GitHub Desktop.
Javascript ordem alfabetica do array
//Javascript ordem alfabetica do array
var objArray = [];
objArray.push({Id: 1, DepartmentName: 'ZMarketing', Active: true});
objArray.push({Id: 2, DepartmentName: 'Sales', Active: true});
objArray.push({Id: 3, DepartmentName: 'Development', Active: true});
objArray.push({Id: 4, DepartmentName: 'Accounting', Active: true});
objArray.sort(function(a, b) {
var textA = a.DepartmentName.toUpperCase();
var textB = b.DepartmentName.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
//Ou
objArray.sort((a, b) => a.DepartmentName.localeCompare(b.DepartmentName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment