- 前言
- Lazy
- createWrapper
- generate
- range
- repeat
- strict
- Sequence
- define
- async
- chunk
- compact
- concat
- consecutive
- contains
- countBy
- dropWhile
- each
- equals
- every
- filter
- find
- findWhere
- first
- flatten
- get
- getIterator
- groupBy
- indexBy
- indexOf
- initial
- intersection
- invoke
- isEmpty
- join
- last
- lastIndexOf
- map
- max
- memoize
- min
- none
- ofType
- pluck
- reduce
- reduceRight
- reject
- rest
- reverse
- shuffle
- size
- some
- sort
- sortBy
- sortedIndex
- sum
- takeWhile
- tap
- toArray
- toObject
- union
- uniq
- where
- without
- zip
- ArrayLikeSequence
- define
- concat
- first
- get
- length
- map
- pop
- push
- rest
- reverse
- shift
- slice
- unshift
- ObjectLikeSequence
- define
- assign
- async
- defaults
- functions
- get
- invert
- keys
- merge
- omit
- pairs
- pick
- toArray
- toObject
- values
- watch
- StringLikeSequence
- define
- charAt
- charCodeAt
- contains
- endsWith
- first
- indexOf
- last
- lastIndexOf
- mapString
- match
- reverse
- split
- startsWith
- substring
- toLowerCase
- toUpperCase
- GeneratedSequence
- each
- length
- AsyncSequence
- contains
- each
- find
- getIterator
- indexOf
- Iterator
- current
- moveNext
- AsyncHandle
- cancel
- onComplete
- onError
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
sortBy
Creates a new sequence with the same elements as this one, but ordered by the results of the given function.
You can pass:
- a string, to sort by the named property
- a function, to sort by the result of calling the function on each element
Signature
Sequence.sortBy = function(sortFn, descending) { /*...*/ }
Sequence.sortBy = function sortBy(sortFn, descending) { sortFn = createComparator(sortFn); if (descending) { sortFn = reverseArguments(sortFn); } return new SortedSequence(this, sortFn); }
Name | Type(s) | Description |
---|---|---|
sortFn | Function | The function to call on the elements in this sequence, in order to sort them. |
descending | boolean | Whether or not the resulting sequence should be in descending order (defaults to |
returns | Sequence | The new sequence. |
Examples
function population(country) { return country.pop; } function area(country) { return country.sqkm; } var countries = [ { name: "USA", pop: 320000000, sqkm: 9600000 }, { name: "Brazil", pop: 194000000, sqkm: 8500000 }, { name: "Nigeria", pop: 174000000, sqkm: 924000 }, { name: "China", pop: 1350000000, sqkm: 9700000 }, { name: "Russia", pop: 143000000, sqkm: 17000000 }, { name: "Australia", pop: 23000000, sqkm: 7700000 } ]; Lazy(countries).sortBy(population).last(3).pluck('name') // sequence: ["Brazil", "USA", "China"] Lazy(countries).sortBy(area).last(3).pluck('name') // sequence: ["USA", "China", "Russia"] Lazy(countries).sortBy(area, true).first(3).pluck('name') // sequence: ["Russia", "China", "USA"]
Benchmarks
var randoms = Lazy.generate(Math.random).take(100).toArray(); Lazy(randoms).sortBy(Lazy.identity).each(Lazy.noop) // lazy _.each(_.sortBy(randoms, Lazy.identity), _.noop) // lodash
Implementation | Ops/second |
---|---|
lazy | |
lodash |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论