-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" }     
    ]
3
  • Any feedback on my answer ? Commented Feb 28, 2022 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; }) Commented Feb 28, 2022 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 Commented Feb 28, 2022 at 20:07

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

12 Comments

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
@JoseManuel Check the update. you can see that Carlos comes first.
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
@JoseManuel It is the case, I have { name: "Carlos", priority: 20, version: "versionZ", }, appearing before { name: "City25", priority: 6, version: "versionA", },
Thanks, it worked for me, there was a small error that generated bad data to the api
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.