-1

I have been looking for a way to sort an array based on 2 conditions:

First: Sort it based on another array, one being like this: This is the json file to read:

let versiones = ['versionZ', 'versionJ', 'versionA', 'versionK', 'versionC']

If it is not present in the versions array, put them last

Second: Then order it based on its priority present within the elements.

Note: The priority will always exist, it may be that in some cases the key: version does not exist or is shown as null.

Here it would be the array that I want to order without complete success:

    [
        { "name": "Juan", "priority": 10, "version": "versionM" },
        { "name": "Manuel", "priority": 5, "version": "versionA" },
        { "name": "Carlos", "priority": 20, "version": "versionJ" },
        { "name": "Raul", "priority": 12, "version": "versionC" }     
    ]
Jose Manuel
  • 91
  • 1
  • 7
  • Any feedback on my answer ? – medilies Feb 28 '22 at 19:39
  • 1
    a.sort(function(e1, e2){ let x = versiones.indexOf(e1.version); let y = versiones.indexOf(e2.version); if (x == -1 && y >= 0) { return 1; } if (y == -1 && x >= 0) { return -1; } if ( x >= 0 && y >= 0 ) { return x - y; } return e1.priority - e2.priority; }) – kmoser Feb 28 '22 at 19:41
  • when sorting by version, it doesn't have to be in alphabetical order, it has to be in the same order that the versions array shows @kmoser – Jose Manuel Feb 28 '22 at 20:07

1 Answers1

1

let LevelOnePriority = ["Z", "J", "A", "K", "C"];

let obj = [
    { name: "Juan", levelTwoPriority: 10, version: "M", },
    { name: "Manuel", levelTwoPriority: 5, version: "L", },
    { name: "Carlos", levelTwoPriority: 20, version: "Z", },
    { name: "Raul", levelTwoPriority: 12, version: "C", },
    { name: "Park22", levelTwoPriority: 7, version: "A", },
    { name: "City25", levelTwoPriority: 6, version: "A", },
];

obj.sort(function(a, b) {
  // "a" is in the highestPriorityArray and "b" isn't
  if (LevelOnePriority.includes(a.version) && !LevelOnePriority.includes(b.version)) {
    return -1;
  }
  // "b" is in the highestPriorityArray and "a" isn't
  if (!LevelOnePriority.includes(a.version) && LevelOnePriority.includes(b.version)) {
    return +1;
  }

  // Both "a" and "b" are in the highestPriorityArray
  if (LevelOnePriority.includes(a.version) && LevelOnePriority.includes(b.version)) {
    // "a" and "b" aren't in the same version
    if (LevelOnePriority.indexOf(a.version) !== LevelOnePriority.indexOf(b.version)) {
      // 
      return LevelOnePriority.indexOf(a.version) - LevelOnePriority.indexOf(b.version);
    }
  }

  return a.levelTwoPriority - b.levelTwoPriority;
});

console.log(obj);
medilies
  • 1,811
  • 1
  • 8
  • 32
  • 1
    what I try is that it first shows all the versionZ and these arrange them in order of priority. Then those of versionJ... and so on The main priority is the version – Jose Manuel Feb 28 '22 at 19:43
  • @JoseManuel Check the update. you can see that `Carlos` comes first. – medilies Feb 28 '22 at 19:54
  • when sorting by version, it doesn't have to be in alphabetical order, it has to be in the same order that the versions array shows – Jose Manuel Feb 28 '22 at 20:07
  • @JoseManuel It is the case, I have `{ name: "Carlos", priority: 20, version: "versionZ", },` appearing before `{ name: "City25", priority: 6, version: "versionA", },` – medilies Feb 28 '22 at 20:09
  • Thanks, it worked for me, there was a small error that generated bad data to the api – Jose Manuel Feb 28 '22 at 20:52
  • That's great to know :D. good luck. I'll edit variables names to make the solution more generic – medilies Feb 28 '22 at 20:59
  • Upvoted for the useful comments – kmoser Feb 28 '22 at 21:11
  • @kmoser The solution you provided in the comments makes me feel so noob XD. I'm trying to find a shorter solution using math. – medilies Feb 28 '22 at 21:13
  • @medilies I'm not sure how much simpler it can get than what you already have. The if/then logic is what makes this as complex as it is, and I don't see math helping there. – kmoser Feb 28 '22 at 21:19
  • @kmoser here is what I'm trying https://jsfiddle.net/q7f82b46/10/ – medilies Feb 28 '22 at 21:28
  • @medilies Why the +2? – kmoser Feb 28 '22 at 21:35
  • @kmoser because not found in array gives -1 that what I turn it to +1. Then the order of factors becomes [NotFound:1, Last:2, ...... First:Max] – medilies Feb 28 '22 at 21:36