实现customSort(table, criteria)函数的主体

问题描述 投票:0回答:1

The first argument of customSort() is an array of JavaScript objects. The function should sort the array on the property given as the second argument criteria in descending order. The property is numerical only.

Will be sorted by customSort(a, 'id') as:

var a = [{ key:6}, {key:9},{key:2},{key:1},{key:12},{key:63},{key:20}, {key:25},{key:13},{key:19},{key:32},{key:70},{key:14},{key:7},{key:8}]

print(display(a, 'key'));

customSort(a, 'key');

print(display(a, 'key')); //Expected: 70,63,32,25,20,19,14,13,12,9,8,7,6,2,1

javascript
1个回答
0
投票
function customSort(table, critera) {

    var value = display(table, critera);
    var result = value.sort((a, b) = >b - a);
    return result.join(",")
}

function print(func) {
    console.log(customSort(func))
}

function display(a, key) {

    var finalvalue = [];
    for (i = 0; i < myarray.length; i++) {
        finalvalue.push(myarray[i].key);

    }

    return finalvalue;
}

var myarray = [{
    key: 6
},
{
    key: 9
},
{
    key: 2
},
{
    key: 1
},
{
    key: 12
},
{
    key: 63
},
{
    key: 20
},
{
    key: 25
},
{
    key: 13
},
{
    key: 19
},
{
    key: 32
},
{
    key: 70
},
{
    key: 14
},
{
    key: 7
},
{
    key: 8
}];

print(display(myarray, 'key'));

customSort(myarray, 'key');
print(display(myarray, 'key'));
© www.soinside.com 2019 - 2024. All rights reserved.