strangely_self_aware / OkCupid Reverse Enemy Sort

// ==UserScript==
// @name OkCupid Reverse Enemy Sort
// @version 1.0.2
// @namespace   okcupid.script
// @author strangely_self_aware
// @description Remove matches below a certain threshold and re-sort matches that pass the threshold so that the highest match/lowest enemy percentages bubble to the top.
// @include https://www.okcupid.com/match
// @grant none
// ==/UserScript==
var enemyThreshold = 5; // matches with enemy percentage greater than this will be removed
var matchThreshold = 90; // matches with match percentage less than this will be removed

function reverseEnemySort(matches) {
  var thresholded = matches.filter(function(match) { return match.percentages.enemy < enemyThreshold && match.percentages.match >= matchThreshold; });
  var returnValue = thresholded.sort(function(a, b) {
	if (a.percentages.match > b.percentages.match) return -1;
    else if (a.percentages.match < b.percentages.match) return 1;
    else if (a.percentages.enemy < b.percentages.enemy) return -1;
    else if (a.percentages.enemy > b.percentages.enemy) return 1;
    else return 0;
  });
  if (returnValue.length === 0) {
    returnValue = [matches[0]];
  }
  return returnValue;
}

function runOnStart() {
  if(!Ok.ResultsStore) { console.log('no ResultsStore!'); }
  var previousFill = Ok.ResultsStore.fillMatches;
  Ok.ResultsStore.fillMatches = function(a) {
    previousFill.call(this, a);
    if (!this.data.placeholderOperations) {
      var before = this.data.matches.length;
      this.data.matches = reverseEnemySort(this.data.matches);
      for (var i = 0; i < this.data.matches.length; i++) {
        this.data.matches[i].keyId = i;
      }
      this.data.matchKeyIndex = this.data.matches.length;
    }
    return this.data.matches;
  };

  var possible = "0123456789";
  var previousPost = OkC.post;
  OkC.post = function(url, payload) {
    if (url === '/match/search') {
      var text = "";
      for (var i=0; i < 19; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
      payload.data.after = btoa(text + ",0,18");
    }
    return previousPost.call(this, url, payload);
  };

  Ok.ResultsStore.onReplaceMatchesCompleted({after: Ok.ResultsStore.data.after}, {paging: {cursors: {current: Ok.ResultsStore.data.matches[0].pageCode, before: Ok.ResultsStore.data.before, after: Ok.ResultsStore.data.after}}, data: reverseEnemySort(Ok.ResultsStore.data.matches)});
}

window.addEventListener ("load", runOnStart);