- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我目前正在使用 underscorejs 对我的 json 排序进行排序。现在我要求使用 underscore.js 进行 ascending
和 descending
排序。我在文档中没有看到任何相同的内容。我怎样才能做到这一点?
最佳答案
您可以使用 .sortBy
,它总是会返回一个升序列表:
_.sortBy([2, 3, 1], function(num) {
return num;
}); // [1, 2, 3]
但您可以使用 .reverse获取方法降序:
var array = _.sortBy([2, 3, 1], function(num) {
return num;
});
console.log(array); // [1, 2, 3]
console.log(array.reverse()); // [3, 2, 1]
或者在处理数字时,在返回中添加一个负号来降序:
_.sortBy([-3, -2, 2, 3, 1, 0, -1], function(num) {
return -num;
}); // [3, 2, 1, 0, -1, -2, -3]
引擎盖下.sortBy
使用内置 .sort([handler])
:
// Default is alphanumeric ascending:
[2, 3, 1].sort(); // [1, 2, 3]
// But can be descending if you provide a sort handler:
[2, 3, 1].sort(function(a, b) {
// a = current item in array
// b = next item in array
return b - a;
});
关于javascript - 如何使用 underscore.js 进行 asc 和 desc 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15137948/
我有一个小程序,我不想再在浏览器中运行,而是想通过 JNLP 作为可下载、自动更新、可离线的 Java 应用程序运行。 我不想保留将其作为小程序运行的能力。 无论是将现有的小程序包装在 JFrame
我认为我没有做任何过于复杂的事情。我正在对一个大型 csv 数据文件进行预排序,因为它充满了按随机时间顺序到达的数据。索引正确,但返回格式不正确。 sortedList=sorted(read
我在GraphicalLayout中添加了一个ImageView,但它在我的实际设备上看起来与在AVD上不同。。我发现我需要在一个布局.xml中添加Content Description,但是当我添加
表格如下: CREATE TABLE `api_stats` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(30) DEFAULT NU
我的表有百万条记录。我只想提取最后 200 行中计数器最多的 5 行。 我正在使用它,但我认为这是两次选择.. 这可能不适合最佳性能,请提出建议。 "select * from (
我正在尝试为 hg log 创建一个自定义模板,其中一部分显示第一行的前 N(例如 72)个字符。基于this answer到目前为止我收到的另一个问题: hg log --template '{de
这里我们有一个带连字符的字符串,如 0-1-3.... 并且长度不固定,我们在hive中还有一个DETAIL表来解释每个代码的含义。 详情 |代码 |描述 | + ---- + ---- + | 0
它应该列出一个列表,并在顶部列出最高百分比..但它没有 https://gyazo.com/ecde864ef09115b8b119eba8a39ecd68这是我运行时的图片 这里是代码。怎么了? $
我正在尝试根据论坛 ID 获取所有主题。这些主题需要首先按粘性排序,然后按最后发布日期排序。 我有这个查询,工作得几乎正常,但它没有按照我想要的方式对主题进行排序。 SELECT forum_
我在数据库中创建了一个表并添加了一些约束。当我在表上使用 DESC 关键字时,根据我放置的约束,“Key”列会更改其记录。 这是表定义: CREATE TABLE t( sif INT, sif
我有一个获取两行的查询 - SELECT tb.type, tb.value, tb.comp, tb.
我在 SQL 中有这个查询 SELECT Book FROM Library ORDER BY Book desc 我想要的排序 [Expired]Web Publishing Co., Ltd. [
我正在使用 DataTables 来显示一些数据,它效果很好,但我想稍微自定义它,但不确定如何。 我想要做的是,当用户单击列标题对该列进行排序时,我希望它最初按降序而不是升序排序。有什么办法可以做到这
我有这个 SQL(SQLite 数据库),它正确地从按 session ID 分组的消息表中获取最新消息,其中消息的 session ID 不是“无”。 SELECT * from messages
我想要 desc 表;操作以显示列的注释。我已经看到有些人做到了这一点,但我不知道如何。也许这取决于 SQL Developer 版本,我的是 2.1.0.63。数据库是Oracle 11g。 这是我
我正在检索 SQL 中的一些数据,按 DESC 排序。然后我想反转结果。我是通过将数据插入数组然后使用 array_reverse 来做到这一点的,但我发现这对 CPU 时间来说非常繁重,并且只想使用
我正在按日期降序从数据库中获取一些数据,并使用shift-push通过循环将其添加到数组中,因为新数据不断定期到达。 我无法理解的一件奇怪的事情是为什么第一个获取的值被添加到最后一个索引。 这是我的代
我有一个问题,我不知道如何解决。我有这个查询: $sql = "SELECT * FROM transfer_centre WHERE status = 'out' ORDER BY pris DES
我有 2 个 MYSQL 表,firma 和 rach 公司表: id_fir | nazwa | opis | nr_konta | logo-------------------------
我希望得到一些帮助:我正在尝试输出一个用户可以通过各种选择字段查看和排列的 mysql 数据表。我希望数据按日期顺序自动排列(最新的在前)并且每页限制为 10 个结果。 我有以下脚本摘录(为简洁起见,
我是一名优秀的程序员,十分优秀!