Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

sorting - Sort array of EmberJS objects by property

I noticed this on the EmberJS website under Filtering:

Another common task to perform on an Enumerable is to take the Enumerable as input, and return an Array after sorting or filtering it based on some criteria.

Imagine I have an array of Ember objects, how do I go about sorting them by property?

App.DemoArray = Ember.ArrayController.create({
    content:[
        Ember.Object.create({name:'Joe', Age:29}),
        Ember.Object.create({name:'Jim', Age:53}),
        Ember.Object.create({name:'Jack', Age:12})
    ]
})

What if I wanted to sort the above by age? Thanks for your help!

EDIT: I found this in the sproutcore documentation but it doesn't seem to work with Ember:

You can sort an Enumerable based on the value of some property or list of properties using sortProperty. If you pass in multiple properties, SproutCore will sort items with the same value for the first property by the value of the second parameter, and so on.

Section 3.8 on this page: http://guides.sproutcore20.com/enumerables.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ember MutableArray has a sortBy method:

https://emberjs.com/api/ember/release/classes/MutableArray


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...