Collect.js sortKeysDesc() 方法
sortKeysDesc() 方法用于按底层关联数组的给定键对集合元素进行降序排序。
句法:
collect(array).sortKeysDesc(key)
参数: collect() 方法采用一个参数,该参数转换为集合,然后将 sortKeysDesc() 方法应用于它。 sortKeysDesc() 方法将键作为参数保存。
返回值:该方法按降序返回排序后的集合元素。
下面的示例说明了 collect.js 中的 sortKeysDesc() 方法:
示例 1:
Javascript
const collect = require("collect.js");
let obj = {
name: "Rahul",
dob: "25-10-96",
section: "A",
score: 98,
};
const collection = collect(obj);
const sorted = collection.sortKeysDesc();
console.log(sorted.all());
Javascript
const collect = require("collect.js");
let obj = {
sts_name: "Rakesh",
id: "1005",
course: "Web Technology",
DOB: "15-12-91",
section: "A",
};
const collection = collect(obj);
const sorted = collection.sortKeysDesc();
console.log(sorted.all());
输出:
{ section: 'A', score: 98, name: 'Rahul', dob: '25-10-96' }
示例 2:
Javascript
const collect = require("collect.js");
let obj = {
sts_name: "Rakesh",
id: "1005",
course: "Web Technology",
DOB: "15-12-91",
section: "A",
};
const collection = collect(obj);
const sorted = collection.sortKeysDesc();
console.log(sorted.all());
输出:
{
sts_name: 'Rakesh',
section: 'A',
id: '1005',
course: 'Web Technology',
DOB: '15-12-91'
}