最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort array of arrays with documents using lodash - Stack Overflow

programmeradmin3浏览0评论

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.

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'

[[{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}]]

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.

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'

[[{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}]]
Share Improve this question edited Mar 15, 2017 at 15:52 Ajay Srikanth asked Mar 15, 2017 at 15:15 Ajay SrikanthAjay Srikanth 1,1854 gold badges24 silver badges44 bronze badges 3
  • Please show the desired output – Weedoze Commented Mar 15, 2017 at 15:16
  • Are A,B,C,D supposed to be strings or other objects? – Fabian Klötzl Commented Mar 15, 2017 at 15:20
  • are the inner arrays always sorted by type? – Nina Scholz Commented Mar 15, 2017 at 15:38
Add a ment  | 

3 Answers 3

Reset to default 2

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

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./ajax/libs/lodash.js/4.17.4/lodash.js"></script>

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

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; }

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]));
发布评论

评论列表(0)

  1. 暂无评论