gpt4 book ai didi

arrays - 如何使用 Lo-Dash 对数组进行降序排序?

转载 作者:行者123 更新时间:2023-12-02 05:24:58 25 4
gpt4 key购买 nike

我有一个来自推送项目的数组,当我使用 Lo-Dash 显示时,它按升序显示。

这是我的代码:

<h3>Sorted Array</h3>
<ul id="sorted"></ul>

// Sample array.
var array = [];

// Utility function used to push data into the
// array while maintaining the sort order.
function sortedPush( array, value ) {
array.splice( _.sortedIndex( array, value ), 0, value );
}

// push items.
sortedPush( array, "20151124_nov_24_2015" );
sortedPush( array, "201511011_nov_1_2015" );
sortedPush( array, "20160118_jan_18_2016" );
sortedPush( array, "201508031_aug_3_2015" );

// The ul for output.
var list = document.getElementById( "sorted" );

// Display the sorted array.
_.each( array, function( item ) {
var li = document.createElement( "li" );
li.appendChild( document.createTextNode( item ) );
list.appendChild( li );
});

输出:

Sorted Array

201508031_aug_3_2015
201511011_nov_1_2015
20151124_nov_24_2015
20160118_jan_18_2016

现在,如何按降序显示它?

预期输出应该是:

20160118_jan_18_2016
20151124_nov_24_2015
201511011_nov_1_2015
201508031_aug_3_2015

这是 fiddle - https://jsfiddle.net/vLvv6ogf/1/

// Sample array.
var array = [];

// Utility function used to push data into the
// array while maintaining the sort order.
function sortedPush(array, value) {
array.splice(_.sortedIndex(array, value), 0, value);
}

// push items.
sortedPush(array, "20151124_nov_24_2015");
sortedPush(array, "201511011_nov_1_2015");
sortedPush(array, "20160118_jan_18_2016");
sortedPush(array, "201508031_aug_3_2015");


// The ul for output.
var list = document.getElementById("sorted");

// Display the sorted array.
_.each(array, function(item) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(item));
list.appendChild(li);
});
<h3>Sorted Array</h3>
<ul id="sorted"></ul>

感谢对此的任何帮助。谢谢。

最佳答案

简单地按升序对数组进行排序,然后反转它

_.reverse(数组)

这里是 lodash 文档链接 _.reverse(array)

关于arrays - 如何使用 Lo-Dash 对数组进行降序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35175054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com