deepg
deepg

Reputation: 403

Sort dictionary containing array as value and rank

I am looking for the best possible, minimum operation to solve this.

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

I want to sort the dictionary based on the integer values contained within each key and then rank each value,as shown in the 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" ]
    ]
}

For example [ "al", 26, "rank-1" ] .This is rank-1 as 26 is maximum among all the other values.

Javascript is the most preferable language.Looking for the best optimal solution

Upvotes: 1

Views: 259

Answers (3)

fafl
fafl

Reputation: 7385

This can be done in a few lines:

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)

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

Since arrays are passed by reference, you can make use of that like this:

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

If you are allowed to use arrow functions:

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

Upvotes: 3

Nenad Vracar
Nenad Vracar

Reputation: 122057

You can first reduce it to array that stores key|index from original object and then sort it and add rank property and then again create object.

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)

Upvotes: 0

Related Questions