使用lodash对具有文档的数组排序

时间:2022-05-31 15:58:23

I've an array with arrays of documents in it, can some help me on how to sort those arrays with multiple documents inside the array. The user can chose of the type to be searched on. If they can choose 'A' sort all the array items by value associated with type 'A', similarly for the other types. What is the best way to do this. Lodash solution would be highly beneficial. Those values can be integers/strings/guids/datetime fields.

我有一个包含文档数组的数组,可以帮助我对数组中包含多个文档的数组进行排序。用户可以选择要搜索的类型。如果他们可以选择“A”,根据与“A”类型关联的值对所有数组项进行排序,其他类型也是如此。最好的办法是什么?Lodash的解决方案是非常有益的。这些值可以是整数/字符串/guids/datetime字段。

Type is always a string, in any case I don't think it does really matter.

类型总是一个字符串,无论如何,我不认为它真的重要。

[
 [{type:A, value:10},{type:B, value:20},{type:C, value:15},{type:D, value:20}],
 [{type:A, value:5},{type:B, value:10},{type:C, value:35},{type:D, value:40}],
 [{type:A, value:30},{type:B, value:30},{type:C, value:25},{type:D, value:30}],
 [{type:A, value:20},{type:B, value:50},{type:C, value:55},{type:D, value:10}]
]

Desired out put if sorted by type 'A'

如果按A类型排序

[[{type:A, value:5},{type:B, value:10},{type:C, value:35},{type:D, value:40}],
     [{type:A, value:10},{type:B, value:20},{type:C, value:15},{type:D, value:20}],
     [{type:A, value:20},{type:B, value:50},{type:C, value:55},{type:D, value:10}],
     [{type:A, value:30},{type:B, value:30},{type:C, value:25},{type:D, value:30}]]

3 个解决方案

#1


2  

Here's a lodash solution that uses sortBy for sorting and find to get the corresponding values with a specific type.

这里有一个lodash解决方案,它使用sortBy进行排序并找到与特定类型对应的值。

var result = _.sortBy(data, function(item) {
  return _.find(item, { type: 'A' }).value;
});

var data = [
  [{
    type: 'A',
    value: 10
  }, {
    type: 'B',
    value: 20
  }, {
    type: 'C',
    value: 15
  }, {
    type: 'D',
    value: 20
  }],
  [{
    type: 'A',
    value: 5
  }, {
    type: 'B',
    value: 10
  }, {
    type: 'C',
    value: 35
  }, {
    type: 'D',
    value: 40
  }],
  [{
    type: 'A',
    value: 30
  }, {
    type: 'B',
    value: 30
  }, {
    type: 'C',
    value: 25
  }, {
    type: 'D',
    value: 30
  }],
  [{
    type: 'A',
    value: 20
  }, {
    type: 'B',
    value: 50
  }, {
    type: 'C',
    value: 55
  }, {
    type: 'D',
    value: 10
  }]
];

var result = _.sortBy(data, function(item) {
  return _.find(item, { type: 'A' }).value;
});

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

#2


2  

In plain Javascript, you could look for the given type and sort by the corresponding value.

在普通的Javascript中,您可以查找给定的类型并按相应的值进行排序。

function sort(data, type) {
    function getValue(array) {
        var value = -Number.MAX_VALUE;
        array.some(function (o) {
            if (o.type === type) {
                value = o.value;
                return true;
            }
        });
        return value;
    }

    data.sort(function (a, b) {
        var aa = getValue(a),
            bb = getValue(b);
        return isFinite(aa) && isFinite(bb) ? aa - bb : aa.localeCompare(bb);
    });
}


var data = [[{ type: 'A', value: 10 }, { type: 'B', value: 20 }, { type: 'C', value: 15 }, { type: 'D', value: 'b' }], [{ type: 'A', value: 5 }, { type: 'B', value: 10 }, { type: 'C', value: 35 }, { type: 'D', value: 'a' }], [{ type: 'A', value: 30 }, { type: 'B', value: 30 }, { type: 'C', value: 25 }, { type: 'D', value: 'd' }], [{ type: 'A', value: 20 }, { type: 'B', value: 50 }, { type: 'C', value: 55 }, { type: 'D', value: 'c' }]];

sort(data, 'A');
console.log(data);
sort(data, 'D');
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


0  

You can dynamically create a relationship for sorting using closures.

您可以动态创建使用闭包进行排序的关系。

function sortBy(key){
    return function(a,b){
        return a[key] - b[key];
    }
}

var index = {'A':0, 'B': 1, … };
var userPickedType = 'A';
yourarray.sort(sortBy(index[userPickedType]));

#1


2  

Here's a lodash solution that uses sortBy for sorting and find to get the corresponding values with a specific type.

这里有一个lodash解决方案,它使用sortBy进行排序并找到与特定类型对应的值。

var result = _.sortBy(data, function(item) {
  return _.find(item, { type: 'A' }).value;
});

var data = [
  [{
    type: 'A',
    value: 10
  }, {
    type: 'B',
    value: 20
  }, {
    type: 'C',
    value: 15
  }, {
    type: 'D',
    value: 20
  }],
  [{
    type: 'A',
    value: 5
  }, {
    type: 'B',
    value: 10
  }, {
    type: 'C',
    value: 35
  }, {
    type: 'D',
    value: 40
  }],
  [{
    type: 'A',
    value: 30
  }, {
    type: 'B',
    value: 30
  }, {
    type: 'C',
    value: 25
  }, {
    type: 'D',
    value: 30
  }],
  [{
    type: 'A',
    value: 20
  }, {
    type: 'B',
    value: 50
  }, {
    type: 'C',
    value: 55
  }, {
    type: 'D',
    value: 10
  }]
];

var result = _.sortBy(data, function(item) {
  return _.find(item, { type: 'A' }).value;
});

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

#2


2  

In plain Javascript, you could look for the given type and sort by the corresponding value.

在普通的Javascript中,您可以查找给定的类型并按相应的值进行排序。

function sort(data, type) {
    function getValue(array) {
        var value = -Number.MAX_VALUE;
        array.some(function (o) {
            if (o.type === type) {
                value = o.value;
                return true;
            }
        });
        return value;
    }

    data.sort(function (a, b) {
        var aa = getValue(a),
            bb = getValue(b);
        return isFinite(aa) && isFinite(bb) ? aa - bb : aa.localeCompare(bb);
    });
}


var data = [[{ type: 'A', value: 10 }, { type: 'B', value: 20 }, { type: 'C', value: 15 }, { type: 'D', value: 'b' }], [{ type: 'A', value: 5 }, { type: 'B', value: 10 }, { type: 'C', value: 35 }, { type: 'D', value: 'a' }], [{ type: 'A', value: 30 }, { type: 'B', value: 30 }, { type: 'C', value: 25 }, { type: 'D', value: 'd' }], [{ type: 'A', value: 20 }, { type: 'B', value: 50 }, { type: 'C', value: 55 }, { type: 'D', value: 'c' }]];

sort(data, 'A');
console.log(data);
sort(data, 'D');
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#3


0  

You can dynamically create a relationship for sorting using closures.

您可以动态创建使用闭包进行排序的关系。

function sortBy(key){
    return function(a,b){
        return a[key] - b[key];
    }
}

var index = {'A':0, 'B': 1, … };
var userPickedType = 'A';
yourarray.sort(sortBy(index[userPickedType]));