0

I have an array that looks like this:

  { username: 'usernameadw',
    rank: 'rookie',
    points: 891
  },
  { username: 'username662',
    rank: 'rookie',
    points: 999
  },
  { username: 'usernameabd8a',
    rank: 'knight',
    points: 2393
  },
  { username: 'usernamev88',
    rank: 'mage',
    points: 1673
  },
  { username: 'abcusername123',
    rank: 'mage',
    points: 1483
  }

I am wondering how I can split these arrays into arrays like "mageArray", "knightArray", rookieArray", and then with each array, sort them by how many points each object has from highest to lowest?

I have tried using this code so far but it's not working. What I tried to do was create a new object from the large array with the info, and then push it to another array if the rank name matched

var obj = new Object({
            username: usersRows[i].username,
            rank: usersRows[i].rank,
            points: usersRows[i].points
          });

if (usersRows[i].rank === 'mage') { mageArray.push(obj); }
Mosukoshide
  • 71
  • 10
  • Why did you tagged it as **mysql**? – nacho Dec 30 '19 at 16:31
  • @nacho I am receiving the array from a mysql query, if that would help solve the question in any way. – Mosukoshide Dec 30 '19 at 16:36
  • sort it in the select staement – fubar Dec 30 '19 at 16:40
  • It seems you have two questions; how to split your array into three separate arrays based on property value ([break array of objects into separate arrays based on a property](https://stackoverflow.com/q/14696326/215552)) and how to sort an array based on property value ([Sorting an array of objects by property values](https://stackoverflow.com/q/979256/215552)) – Heretic Monkey Dec 30 '19 at 16:42
  • Does this answer your question? [break array of objects into separate arrays based on a property](https://stackoverflow.com/questions/14696326/break-array-of-objects-into-separate-arrays-based-on-a-property) – Heretic Monkey Dec 30 '19 at 16:43

2 Answers2

0

Try this.

const usersRows = [{
    username: 'usernameadw',
    rank: 'rookie',
    points: 891
},
{
    username: 'username662',
    rank: 'rookie',
    points: 999
},
{
    username: 'usernameabd8a',
    rank: 'knight',
    points: 2393
},
{
    username: 'usernamev88',
    rank: 'mage',
    points: 1673
},
{
    username: 'abcusername123',
    rank: 'mage',
    points: 1483
}];


function makeSortMap(usersRows) {
    const sortMap = {};
    for(row of usersRows) {
        const key = row.rank;
        if(!sortMap[key]) {
            sortMap[key] = [];
        }

        sortMap[key].push(row);
    }

    for(r in sortMap) {
        sortMap[r].sort((a, b) => b.points - a.points);
    }

    return sortMap;
}

makeSortMap(usersRows);

sort reference: Sorting object property by values

seunggabi
  • 1,699
  • 12
  • 12
0

First let's break up the main array into three sub arrays using the following:

var users = [{ username: 'usernameadw',
    rank: 'rookie',
    points: 891
  },
  { username: 'username662',
    rank: 'rookie',
    points: 999
  },
  { username: 'usernameabd8a',
    rank: 'knight',
    points: 2393
  },
  { username: 'usernamev88',
    rank: 'mage',
    points: 1673
  },
  { username: 'abcusername123',
    rank: 'mage',
    points: 1483
  }];

  var rookieArray = [];
  var knightArray = [];
  var mageArray = [];

  // split users into category arrays.
  for (i=0; i<users.length; i++) {

      if (users[i].rank === 'rookie') {
        rookieArray.push(users[i]);
      }

      if (users[i].rank === 'knight') {
        knightArray.push(users[i]);
      }

      if (users[i].rank === 'mage') {
        mageArray.push(users[i]);
        }      
  }

Once we have that, we just need to sort numerically using the array.sort with a compare function like so:

// sort the rookies array
  rookieArray.sort(function(a, b) {
    return b.points - a.points;
  });

  knightArray.sort(function(a, b) {
    return b.points - a.points;
  });

  mageArray.sort(function(a, b) {
    return b.points - a.points;
  });

A good reference on for loops is here: https://www.w3schools.com/js/js_loop_for.asp

A good reference on array.sort is here: https://www.w3schools.com/js/js_array_sort.asp

Nebri
  • 773
  • 2
  • 8
  • 21