Skip to content
Advertisement

How to sort an array with special conditions

I just want to return an array containing the first and second objects based on the “point” property of an object

and I have a condition below, When the condition is satisfied it will swap places.

Default:

ENGLISH[0]
ENGLISH[1]
MATH[0]
MATH[1]
HISTORY[0]
HISTORY[1]
...

if(ENGLISH[0].POINT/ENGLISH[1].POINT > MATH[0].POINT/MATH[1].POINT)

    MATH[0]
    MATH[1]    
    ENGLISH[0]
    ENGLISH[1]
    HISTORY[0]
    HISTORY[1]
    ...

Given the following object:

const arr = [
  { name: "John", age: 32, group: "Math", point: 70 },
  { name: "David", age: 23, group: "Math", point: 20 },
  { name: "Justin", age: 28, group: "Math", point: 20 },
  { name: "Neymar", age: 30, group: "Math", point: 50 },
  { name: "Arnauld", age: 35, group: "History", point: 40 },
  { name: "Ivan", age: 18, group: "History", point: 50 },
  { name: "Nekko", age: 13, group: "History", point: 80 },
  { name: "Lena", age: 25, group: "English", point: 90 },
  { name: "Test", age: 45, group: "English", point: 30 },
  { name: "Ann", age: 19, group: "English", point: 38 }
]

the result I want is

const result = [
  { name: "Lena", age: 25, group: "English", point: 90 },
  { name: "Test", age: 45, group: "English", point: 30 },
  { name: "John", age: 32, group: "Math", point: 70 },
  { name: "David", age: 23, group: "Math", point: 20 },
  { name: "Arnauld", age: 35, group: "History", point: 40 },
  { name: "Ivan", age: 18, group: "History", point: 50 },
  { name: "Ann", age: 19, group: "English", point: 38 },
  { name: "Justin", age: 28, group: "Math", point: 20 },
  { name: "Neymar", age: 30, group: "Math", point: 50 },
  { name: "Nekko", age: 13, group: "History", point: 80 }
]

Advertisement

Answer

You could group the data with an object for the indices and an array of references.

const
    order = { English: 0, Math: 1, History: 2 },
    data = [{ name: "John", age: 32, group: "Math", point: 70 }, { name: "David", age: 23, group: "Math", point: 20 }, { name: "Justin", age: 28, group: "Math", point: 20 }, { name: "Neymar", age: 30, group: "Math", point: 50 }, { name: "Arnauld", age: 35, group: "History", point: 40 }, { name: "Ivan", age: 18, group: "History", point: 50 }, { name: "Nekko", age: 13, group: "History", point: 80 }, { name: "Lena", age: 25, group: "English", point: 90 }, { name: "Test", age: 45, group: "English", point: 30 }, { name: "Ann", age: 19, group: "English", point: 38 }],
    references = [0, 0, 0],
    result = data
        .reduce((r, o) => {
            const i = order[o.group];
            if (r[references[i]][i].length === 2) {
                references[i]++;
                r[references[i]] ??= []
                r[references[i]][i] = [];
            }
            r[references[i]][i].push(o);
            return r;
        }, [[[], [], []]])
        .flat(Infinity);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement