- Underscore.JS 教程
- Underscore.JS - 主页
- Underscore.JS - 概述
- Underscore.JS - 设置环境
- Underscore.JS - 遍历集合
- Underscore.JS - 处理集合
- Underscore.JS - 遍历数组
- Underscore.JS - 处理数组
- Underscore.JS - 函数
- Underscore.JS - 映射对象
- Underscore.JS - 更新对象
- Underscore.JS - 比较对象
- Underscore.JS - 实用工具
- Underscore.JS - 链式调用
- Underscore.JS 实用资源
- Underscore.JS - 快速指南
- Underscore.JS - 实用资源
- Underscore.JS - 讨论
Underscore.JS - sortBy 方法
语法
_.sortBy(list, iteratee, [context])
sortBy 方法通过运行提供的 iteratee 方法获取排序的列表。
示例
var _ = require('underscore');
var list = [{name: 'Sam', age: 10}, {name: 'Joe', age: 12}, {name: 'Rob', age: 15}]
//Example 1. invoke sortBy method to get sorted list of students
var result = _.sortBy(list, 'name');
console.log(result);
list = [1, 4, 6, 3, 7, 9]
//Example 2. invoke sortBy method to get sorted list of number
result = _.sortBy(list)
console.log(result)
将以上程序保存在 tester.js 中。运行以下命令以执行该程序。
命令
\>node tester.js
输出
[
{ name: 'Joe', age: 12 },
{ name: 'Rob', age: 15 },
{ name: 'Sam', age: 10 }
]
[ 1, 3, 4, 6, 7, 9 ]
underscorejs_processing_collection
广告