How do I sort an array with priority?

  • 0
    Tell me how to sort and select from an array. How can I sort in the getSortBy function so that it first returns double groups for which source is 1 and 0, if there are no such groups, then for triple groups where source is 1,1 and 0, if there are no such groups, then for triple groups where source is 2, 2 and 0, if there are no triples, then to return the object whose source is 0. I apologize right away for the fact that maybe I did not formulate the question in that way, or perhaps the question seems like a task. Just explain me please.
    const arr = [
       {id: 1, groupId: 100, source: 1},
      {id: 2, groupId: 100, source: 2},
      {id: 3, groupId: 100, source: 1},
      {id: 4, groupId: 200, source: 1},
      {id: 5, groupId: 200, source: 0},
      {id: 6, groupId: 300, source: 1},
      {id: 7, groupId: 300, source: 0},
      {id: 8, groupId: 300, source: 1},
      {id: 9, groupId: 400, source: 1},
      {id: 10, groupId: 400, source: 0},
      {id: 11, groupId: 400, source: 0},
      {id: 12, groupId: 500, source: 2},
      {id: 13, groupId: 500, source: 1},
    ];
    function getSortBy() {
     
    }
    JavaScript Anonymous, Jul 1, 2020

  • 2 Answers
  • 0
    Flexible



    function getSortBy(arr, rules = ['10', '110', 220]) {
    const groupedMap = arr.reduce((agg, v) => {
    if (!agg[v.groupId]) agg[v.groupId] = []
    agg[v.groupId].push(v)
    return agg
    }, {})

    const groups = Object
    .values(groupedMap)
    .map(arr => arr.sort((a, b) => b.source - a.source))

    const indexedGroups =
    groups
    .map(arr => ({
    arr,
    sortIndex: (rules.indexOf(arr.map(v => v.source).join('')) + 1) || 1000
    }))

    const sortedGroups =
    indexedGroups.sort((a, b) => a.sortIndex - b.sortIndex)

    const firstGroup = sortedGroups[0].arr
    const element = firstGroup.find(v => v.source === 0)

    return element
    }

    const arr = [
    {id: 1, groupId: 100, source: 1},
    {id: 2, groupId: 100, source: 2},
    {id: 3, groupId: 100, source: 1},
    {id: 4, groupId: 200, source: 1},
    {id: 5, groupId: 200, source: 0},
    {id: 6, groupId: 300, source: 1},
    {id: 7, groupId: 300, source: 0},
    {id: 8, groupId: 300, source: 1},
    {id: 9, groupId: 400, source: 1},
    {id: 10, groupId: 400, source: 0},
    {id: 11, groupId: 400, source: 0},
    {id: 12, groupId: 500, source: 2},
    {id: 13, groupId: 500, source: 1},
    ];

    console.log(getSortBy(arr)) // { groupId: 200, id: 5, source: 0 }

    const arr2 = [
    {id: 1, groupId: 100, source: 1},
    {id: 2, groupId: 100, source: 2},
    {id: 3, groupId: 100, source: 1},
    {id: 4, groupId: 200, source: 1},
    {id: 5, groupId: 200, source: 2},
    {id: 6, groupId: 300, source: 1},
    {id: 7, groupId: 300, source: 0},
    {id: 8, groupId: 300, source: 1},
    {id: 9, groupId: 400, source: 1},
    {id: 10, groupId: 400, source: 2},
    {id: 11, groupId: 400, source: 2},
    {id: 12, groupId: 500, source: 2},
    {id: 13, groupId: 500, source: 1},
    ];

    console.log(getSortBy(arr2)) // { groupId: 300, id: 7, source: 0 }
    Anonymous

  • 0
    The first step is to translate this array into an auxiliary structure:

    {
    100: // groupId
    {
    1: [1, 3], // source: [id, id, .....]
    2: [2]
    },
    ......
    }


    Then it will be easier for you to take metrics for sorting: consider which sources the group has (how many, which do not), and which id are included in the source.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!