2017-02-24 144 views
1

我正在寻找最佳解决此问题的最佳解决方案。将包含数组的字典排序为值和排名

var sourceDictionary = { 
    "200" : [ 
     [ "a", 5 ], 
     [ "al", 6 ], 
     [ "xl", 8 ] 
    ], 
    "201" : [ 
     [ "b", 2 ], 
     [ "al", 16 ], 
     [ "al", 26 ], 
     [ "al", 9 ], 
     [ "al", 3 ] 
    ], 
    "202" : [ 
     [ "lm", 7 ] 
    ] 
} 

我要排序基于包含在每个键的整数值的字典,然后排列每一个值,如图中outputputDictionary。

var targetDictionary = { 
    "200" : [ 
     [ "a", 5, "rank-7" ], 
     [ "al", 6, "rank-6" ], 
     [ "xl", 8, "rank-4" ] 
    ], 
    "201" : [ 
     [ "b", 2, "rank-9" ], 
     [ "al", 16, , "rank-2" ], 
     [ "al", 26, "rank-1" ], 
     [ "al", 9, "rank-3" ], 
     [ "al", 3, "rank-8" ] 
    ], 
    "202" : [ 
     [ "lm", 7, "rank-5" ] 
    ] 
} 

例如[ "al", 26, "rank-1" ]。这是等级1,因为26是所有其他值中最大的等级。

JavaScript是最好最佳的解决方案,最优选language.Looking

+3

你有没有尝试这种解决自己呢? – zfrisch

+1

我的方法很正常...循环访问字典,将所有数组收集在一个变量中(并保留引用),然后对它们进行排序,分配等级并将其转换回愿意的目标结构......其中很多操作,如果我认为字典中有五十万个键,我的方法真的很糟糕......因此寻找任何有更好方法的人 – gully

+0

你可能有一个错误,没有'rank-7'。它是故意的还是错误的? –

回答

3

因为数组是通过引用传递,你可以这样使用的是:

function rankify(obj) { 
 
    // PHASE 1: get a reference of all the sub-arrays 
 
    var references = []; 
 
    for(var key in obj) {    // for each key in the object obj 
 
    obj[key].forEach(function(e) { // for each element e (sub-array) of the array obj[key] 
 
     references.push(e);    // push a reference of that array into reference array 
 
    }); 
 
    } 
 
    
 
    // PHASE 2: sort the references 
 
    references.sort(function(a, b) { // sort the items 
 
    return b[1] - a[1];    // to reverse the sort order (a[1] - b[1]) 
 
    }); 
 
    
 
    // PHASE 3: assign the ranks 
 
    references.forEach(function(e, i) { // for each array in the reference array 
 
    e.push("rank-" + (i + 1));  // push another item ("rank-position") where the position is defined by the sort above 
 
    }); 
 
} 
 

 

 
var sourceDictionary = {"200" : [[ "a", 5 ],[ "al", 6 ],[ "xl", 8 ]],"201" : [[ "b", 2 ],[ "al", 16 ],[ "al", 26 ],[ "al", 9 ],[ "al", 3 ]],"202" : [[ "lm", 7 ]]}; 
 

 
rankify(sourceDictionary); 
 
console.log(sourceDictionary);

如果您可以使用箭头功能:

function rankify(obj) { 
 
    Object.keys(obj) 
 
     .reduce((ref, k) => ref.concat(obj[k]), []) // get the references array 
 
     .sort((a, b) => b[1] - a[1])     // sort it 
 
     .forEach((e, i) => e.push("rank-" + (i + 1))); // assign the rank 
 
} 
 

 

 
var sourceDictionary = {"200" : [[ "a", 5 ],[ "al", 6 ],[ "xl", 8 ]],"201" : [[ "b", 2 ],[ "al", 16 ],[ "al", 26 ],[ "al", 9 ],[ "al", 3 ]],"202" : [[ "lm", 7 ]]}; 
 

 
rankify(sourceDictionary); 
 
console.log(sourceDictionary);

+0

将此标记为最佳解决方案,因为与其他解决方案相比,执行/性能时间最短... – gully

0

这可以在几行来:

var sourceDictionary = { 
 
    "200" : [ 
 
     [ "a", 5 ], 
 
     [ "al", 6 ], 
 
     [ "xl", 8 ] 
 
    ], 
 
    "201" : [ 
 
     [ "b", 2 ], 
 
     [ "al", 16 ], 
 
     [ "al", 26 ], 
 
     [ "al", 9 ], 
 
     [ "al", 3 ] 
 
    ], 
 
    "202" : [ 
 
     [ "lm", 7 ] 
 
    ] 
 
} 
 

 
var flatten = arr => [].concat.apply([], arr) 
 
var ranks = flatten(Object.keys(sourceDictionary) 
 
    .map(k => sourceDictionary[k].map(t => t[1])) 
 
) 
 
    .sort((a, b) => b - a) 
 
    .filter(function(item, index, inputArray) { 
 
     // remove duplicates 
 
     return inputArray.indexOf(item) == index; 
 
    }); 
 

 
Object.keys(sourceDictionary) 
 
    .forEach(k => sourceDictionary[k] 
 
    .forEach(t => t.push("rank-" + (1 + ranks.indexOf(t[1]))))) 
 

 
console.log(sourceDictionary)

+2

而不是按照其他顺序('b - a')排序然后反转排序。 –

+0

啊好点:) – fafl

0

您可以首先将其减少到阵列,从原来的对象存储key|index,然后排序,并添加排名属性,然后再次创建对象。

var data = { 
 
    "200" : [ [ "a", 5 ], [ "al", 6 ], [ "xl", 8 ] ], 
 
    "201" : [ [ "b", 2 ], [ "al", 16 ], [ "al", 26 ], [ "al", 9 ], [ "al", 3 ] ], 
 
    "202" : [ [ "lm", 7 ] ] 
 
} 
 

 
var o = Object.keys(data).reduce(function(r, e) { 
 
    data[e].forEach((a, i) => r.push([e + '|' + i, a])) 
 
    return r; 
 
}, []) 
 

 
o.sort((a, b) => b[1][1] - a[1][1]).map(function(e, i) { 
 
    e[1][2] = 'rank-' + (i + 1) 
 
}) 
 

 
var result = o.reduce(function(r, e) { 
 
    var key = e[0].split('|') 
 
    if (!r[key[0]]) r[key[0]] = [] 
 
    r[key[0]][key[1]] = e[1] 
 
    return r 
 
}, {}) 
 

 
console.log(result)

+0

为什么你会用'|'字符来加入键和索引,而不是单独存储它们? – Bergi