Home > Software design >  React Native - sort array based off values in common
React Native - sort array based off values in common

Time:04-01

Hey guys I have the following array that's used to display a flatlist within my app.

Array [
    Object {
        "data": "Item 1",
        "id": "1",
        "subjects": "1,8,9,23,11,15,16,14,20",
    },
    Object {
        "data": "Item 2",
        "id": "2",
        "subjects": "8,11,2,4,16,19",
    },
    Object {
        "data": "Item 3",
        "id": "3",
        "subjects": "16,20,14,11,9,2",
    },
    Object {
        "data": "Item 4",
        "id": "4",
        "subjects": "1,16,19",
    },
]

However I would like to sort this array based off the subjects value. In the app the user can select a couple of subjects which are represented by numbers so lets say the users selected subjects are:

11, 4, 2, 1

I would like to sort the array so that the items with 3 or more subjects in common with the user are sorted to the top and then items with two and then 1 and then none so the array above should look like this after sorting:

Array [
    Object {
        "data": "Item 2",
        "id": "2",
        "subjects": "8,11,2,4,16,19",
    },
    Object {
        "data": "Item 1",
        "id": "1",
        "subjects": "1,8,9,23,11,15,16,14,20",
    },
    Object {
        "data": "Item 3",
        "id": "3",
        "subjects": "16,20,14,11,9,2",
    },
    Object {
        "data": "Item 4",
        "id": "4",
        "subjects": "0,16,19",
    },
]

How can I achieve this?

I have been searching around the array sort function:

Array.prototype.sort()

However I have only seen how to sort based off number comparisons I have never seen an array sorted based off values in common. Please could someone help me with this!

EDIT

Array [
    Object {
        "data": "Item 2",
        "id": "2",
        "subjects": "8,11,2,4,16,19",
        "ranking": "green",
    },
    Object {
        "data": "Item 1",
        "id": "1",
        "subjects": "1,8,9,23,11,15,16,14,20",
        "ranking": "amber",
    },
    Object {
        "data": "Item 3",
        "id": "3",
        "subjects": "16,20,14,11,9,2",
        "ranking": "amber",
    },
    Object {
        "data": "Item 4",
        "id": "4",
        "subjects": "0,16,19",
        "ranking": "red",
    },
]

CodePudding user response:

You could create an object with counts of selected subjects and sort descending by this value.

const
    data = [{ data: "Item 1", id: "1", subjects: "1,8,9,23,11,15,16,14,20" }, { data: "Item 2", id: "2", subjects: "8,11,2,4,16,19" }, { data: "Item 3", id: "3", subjects: "16,20,14,11,9,2" }, { data: "Item 4", id: "4", subjects: "1,16,19" }],
    selected = [11, 4, 2, 1],
    counts = data.reduce((r, { id, subjects }) => {
        r[id] = subjects
            .split(',')
            .reduce((s, v) => s   selected.includes( v), 0);
        return r;
    }, {});

data.sort((a, b) => counts[b.id] - counts[a.id]);
console.log(data);
console.log(counts);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related