Arrays 如何使用Lo破折号按降序排列数组?
我有一个推送项目的数组,当我使用Lo Dash显示时,它以升序显示 这是我的密码:Arrays 如何使用Lo破折号按降序排列数组?,arrays,lodash,Arrays,Lodash,我有一个推送项目的数组,当我使用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 sorted
<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 );
});
排序数组
如果数组是按升序排序的,并且您希望输出是按降序排序的,只需将调用替换为。这将以相反的方向遍历数组。简单地按升序排序数组,然后将其反转
_.反向(数组)
这里是lodash文档链接