Last active
May 19, 2020 16:57
-
-
Save matheusmurta/c759c3c48af45b905bb7fe3572ac4df3 to your computer and use it in GitHub Desktop.
Javascript ordem alfabetica do array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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