Sort items in an array based on a number of conditions

I have an array of correct ratings that looks like this:

var correctScores = [
   {score: "2-2", code: "draw"},
   {score: "2-1", code: "home"},
   {score: "0-1", code: "away"},
   {score: "1-0", code: "home"},
   {score: "3-0", code: "home"},
   {score: "any home win", code: "home"},
   {score: "1-2", code: "away"},
   {score: "0-3", code: "away"},
   {score: "any draw", code: "draw"},
   {score: "0-0", code: "draw"},
   {score: "any away win", code: "away"},
   {score: "0-2", code: "away"},
   {score: "1-1", code: "draw"},
   {score: "2-0", code: "home"}
]

I was able to sort them by home, draw, awaybut I'm also trying to spread it to the account to be in sequential order. When I talk about sequential order, I want the ratings to be ordered so that the least number of goals is the first in the array. After that, if there are the same number of goals, the game with more home goals should be shown first.

home, draw, away. , . , , .

let order = { HOME: 1, DRAW: 2, AWAY: 3 };

correctScores.sort(function (a, b) { return order[a.code] - order[b.code]; });

:

var correctScores = [
   {score: "1-0", code: "home"},
   {score: "2-0", code: "home"},
   {score: "3-0", code: "home"},
   {score: "2-1", code: "home"},
   {score: "any home win", code: "home"}

   {score: "0-0", code: "draw"},
   {score: "1-1", code: "draw"},
   {score: "2-2", code: "draw"},
   {score: "any draw", code: "draw"},

   {score: "0-1", code: "away"},
   {score: "0-2", code: "away"},
   {score: "0-3", code: "away"},
   {score: "1-2", code: "away"},
   {score: "any away win", code: "away"}

]

- - , ?

+4
3

, .

1 .

var correctScores = [{ score: "2-2", code: "draw" }, { score: "2-1", code: "home" }, { score: "0-1", code: "away" }, { score: "1-0", code: "home" }, { score: "3-0", code: "home" }, { score: "1-2", code: "away" }, { score: "0-3", code: "away" }, { score: "0-0", code: "draw" }, { score: "0-2", code: "away" }, { score: "1-1", code: "draw" }, { score: "2-0", code: "home" }],
    order = { home: 1, draw: 2, away: 3 };

correctScores.sort(function (a, b) {
    function getParts(s) { return s.split('-'); }
    var aa = getParts(a.score),
        bb = getParts(b.score);

    return order[a.code] - order[b.code] || aa[1] - bb[1] || aa[0] - bb[0];
});

console.log(correctScores);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hide result

, , Infinite , .

var correctScores = [{ score: "2-2", code: "draw" }, { score: "2-1", code: "home" }, { score: "0-1", code: "away" }, { score: "1-0", code: "home" }, { score: "3-0", code: "home" }, { score: "any home win", code: "home" }, { score: "1-2", code: "away" }, { score: "0-3", code: "away" }, { score: "any draw", code: "draw" }, { score: "0-0", code: "draw" }, { score: "Any away win", code: "away" }, { score: "0-2", code: "away" }, { score: "1-1", code: "draw" }, { score: "2-0", code: "home" }],
    order = { home: 1, draw: 2, away: 3 };

correctScores.sort(function (a, b) {
    function getParts(s) {
        var t = s.split('-');
        return t.length === 1 ? [Infinity, Infinity] : t;
    }
    var aa = getParts(a.score),
        bb = getParts(b.score);

    return order[a.code] - order[b.code] || aa[1] - bb[1] || aa[0] - bb[0];
});

console.log(correctScores);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hide result
+5

var correctScores = [
   {score: "2-1", code: "home"},
   {score: "1-0", code: "home"},
   {score: "3-0", code: "home"},
   {score: "2-0", code: "home"},

   {score: "1-1", code: "draw"},
   {score: "0-0", code: "draw"},
   {score: "2-2", code: "draw"},

   {score: "0-3", code: "away"},
   {score: "0-2", code: "away"},
   {score: "0-1", code: "away"},
   {score: "1-2", code: "away"}

];

var groups = {};
var scores = [];

correctScores.map(function(e) { 
  groups[e.code] = groups[e.code] || [];
  groups[e.code].push(e);
});

for(var i in groups) {
  groups[i].sort(function (a, b) {
    var scoreA = a.score.split('-').map(Number);
    var scoreB = b.score.split('-').map(Number);
    
    return scoreA[0]+scoreA[1] > scoreB[0]+scoreB[1] ? 1:(
     scoreA[0]+scoreA[1] < scoreB[0]+scoreB[1] ? -1:0);
  });
  scores = scores.concat(groups[i]);
}

console.log(scores);
Hide result
0

Including new requirement:

  var correctScores = [
  {score: "2-2", code: "draw"},
  {score: "2-1", code: "home"},
  {score: "0-1", code: "away"},
  {score: "1-0", code: "home"},
  {score: "3-0", code: "home"},
  {score: "Any home win", code: "home"},
  {score: "1-2", code: "away"},
  {score: "0-3", code: "away"},
  {score: "any draw", code: "draw"},
  {score: "0-0", code: "draw"},
  {score: "Any away win", code: "away"},
  {score: "0-2", code: "away"},
  {score: "1-1", code: "draw"},
  {score: "2-0", code: "home"}
];

let order = { home: 1, draw: 2, away: 3 };

correctScores.sort(function (a, b) {
  s1 = parseInt(a.score.split("-")[0]);
  s2 = parseInt(b.score.split("-")[0]);
  s1s = s1+parseInt(a.score.split("-")[1]);
  s2s = s2+parseInt(b.score.split("-")[1]);
  if(a.score.toLowerCase().indexOf("any")>-1){s1=999;s1s=999;}
  if(b.score.toLowerCase().indexOf("any")>-1){s2=999;s2s=999;}
  if(order[a.code]<order[b.code]) return -1;
  if(order[a.code]>order[b.code]) return 1;
  if(s1s<s2s) return -1;
  if(s1s>s2s) return 1;
  if(s1>s2) return -1;
  if(s1<s2) return 1;  
});

console.log(correctScores);

Fiddle: https://jsfiddle.net/ct6knnqb/1/

0
source

Source: https://habr.com/ru/post/1680300/


All Articles