kdweber89 kdweber89 - 1 year ago 83
Javascript Question

Sorting angular objects alphabetically

I have a drop down arrow, but all of the elements within the drop down arrow are not sorted in a healthy way. I'm trying to use the

angular filter, but have been having some struggles. After doing a little bugging, I believe that the problem is because what i'm needing to have displayed is nested within some objects.

My front end looks like

%input-md{ type: 'select', "ng-model" => "vm.form.group", required: true, options: 'vm.groups', placeholder: 'Select your group' }

My controller has this function in it that addresses the groups.

init = ->
success = (groups) ->
vm.groups = groups

So I first threw in a debugger in the function right after
gets defined, upon which I searched for groups in the JS console, and received a number or objects that looked like this.

text : "American group"
value : Object
__proto__ : Object

Looks kinda like:
enter image description here

I need these to be sorted by their
text field
so in this case
American group

I tried going at

%input-md{ type: 'select', "ng-model" => "vm.form.group", required: true, options: "object in vm.groups | orderBy: 'text'", placeholder: 'Select your group' }

But I keep getting an error not recognizing
. Its
Failed to execute 'setAttribute' on 'Element': 'text'' is not a valid attribute name.

So I'm not sure what I am missing, and i'm hoping someone can take a quick look and hopefully straighten me out a bit.

***** Edit ****

After doing some further research, I've learned that orderBy() does not work with objects, it only works with arrays. Which explains why my approach was not working. (still trying to figure out how to actually go about with a fix like this)

Answer Source

Change your options attribute to: object.text for object in data | orderBy: 'text'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download