Match Max item from list of list items

by Shishir Morshed

JavaScript

var self = ['a','b','c','d','e'];
  var all = [
    ['f','b','3','5','e'],
    ['a','1','c','d','f'],
    ['a','b','1','d','e'],
    ['5','b','c','2','e'],
    ['a','3','c','2','e'],
    ['a','b','4','d','e'],
    ['5','b','f','d','e']
  ]

  function checkMatch(a){
    var result = [];
    // var sortIndex = {};
    var sortIndex = [];

    for(var i=0; i<a.length; i++){
      var matchCount = 0;
      for(var j=0; j<a[i].length; j++){
        if(self.indexOf(a[i][j]) > -1){
          matchCount++;
        }
      };
      // sortIndex[i]=matchCount;
      sortIndex.push([i,matchCount])
    }
    sortIndex.sort(function(a, b) {return a[1] - b[1]});

    for(var i= sortIndex.length-1; i>-1; i--){
      result.push(a[sortIndex[i][0]]);
      console.log(a[sortIndex[i][0]]);
      console.log(sortIndex[i][1]);
    }
    console.log(result);
  }

  checkMatch(all);