0

I am building an Angular 9 app. In this app I need to sort one array using another array.

This is the first array:

[{
    "field_key": "title",
    "field_type": "text",
    "field_label": "Title",
    "rows": [{
        "id": 267255299,
        "content": "Personal task"
    }, {
        "id": 568449863,
        "content": "Super"
    }, {
        "id": 110557130,
        "content": "Another child"
    }, {
        "id": 365047528,
        "content": "Another task"
    }, {
        "id": 383976941,
        "content": "Nice"
    }, {
        "id": 811735335,
        "content": "Arrays"
    }, {
        "id": 324862914,
        "content": "Arrays"
    }, {
        "id": 485226870,
        "content": "Yay a child"
    }, {
        "id": 276334971,
        "content": "My new task"
    }, {
        "id": 549277506,
        "content": "Douplicatee"
    }]
}, {
    "field_key": "progress",
    "field_type": "progress",
    "field_label": "Progress",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": 100
    }, {
        "id": 365047528,
        "content": 100
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": 100
    }, {
        "id": 485226870,
        "content": null
    }, {
        "id": 276334971,
        "content": 60
    }, {
        "id": 549277506,
        "content": 0
    }]
}, {
    "field_key": "ends_at_date",
    "field_type": "date",
    "field_label": "Due date",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": null
    }, {
        "id": 365047528,
        "content": null
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": null
    }, {
        "id": 485226870,
        "content": "2020-08-25"
    }, {
        "id": 276334971,
        "content": "2020-08-16"
    }, {
        "id": 549277506,
        "content": null
    }]
}]

This is the array I want to use to sort the first array. The "key" key in the second array is the same value as the "field_key" in the first array. So I guess I should be able to use these values to do the sorting.

[{
      key: 'title',
      label: 'Title',
      field: 'text',
      visible: true
    }, {
      key: 'ends_at_date',
      label: 'Due date',
      field: 'date',
      visible: true
    }, {
      key: 'progress',
      label: 'Progress',
      field: 'progress',
      visible: true
    }]

I want to reorder/sort the first array so that it is in the same order as the last array based upon the key/field_key values. Right now the order of the first array is: Title, Progress, Ends_at_date but the order of the second array (the correct one) is Title, Ends_at_date, Progress.

3
  • Do you really mean sorting or do you maybe mean filtering? I really don't get what you want to sort here. Commented Aug 28, 2020 at 9:19
  • I want to reorder/sort the first array so that it is in the same order as the last array based upon the key/field_key values. Right now the order of the first array is: Title, Progress, Ends_at_date but the order of the second array (the correct one) is Title, Ends_at_date, Progress. Commented Aug 28, 2020 at 9:21
  • So, all the side data doesn't matter, you just want the indexes of the first array to match the order of appearance in the second array? Commented Aug 28, 2020 at 9:39

2 Answers 2

1

Based on your comment you requirements are now clearer. Please try

const array1 = [{
    "field_key": "title",
    "field_type": "text",
    "field_label": "Title",
}, {
    "field_key": "progress",
    "field_type": "progress",
    "field_label": "Progress",
}, {
    "field_key": "ends_at_date",
    "field_type": "date",
    "field_label": "Due date",
}];

const array2 = [{
      key: 'title',
      label: 'Title',
      field: 'text',
      visible: true
    }, {
      key: 'ends_at_date',
      label: 'Due date',
      field: 'date',
      visible: true
    }, {
      key: 'progress',
      label: 'Progress',
      field: 'progress',
      visible: true
    }];

const sorted = array1.sort((left, right) => array2.findIndex(sortKey => sortKey.key === left.field_key) - array2.findIndex(sortKey => sortKey.key === right.field_key));

console.log(sorted);
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to do an in-place sort, I guess you could just get the indexes of the different keys, and then just set the indexes of a resulting array as in the example below

const array1 = [{
    "field_key": "title",
    "field_type": "text",
    "field_label": "Title",
    "rows": [{
        "id": 267255299,
        "content": "Personal task"
    }, {
        "id": 568449863,
        "content": "Super"
    }, {
        "id": 110557130,
        "content": "Another child"
    }, {
        "id": 365047528,
        "content": "Another task"
    }, {
        "id": 383976941,
        "content": "Nice"
    }, {
        "id": 811735335,
        "content": "Arrays"
    }, {
        "id": 324862914,
        "content": "Arrays"
    }, {
        "id": 485226870,
        "content": "Yay a child"
    }, {
        "id": 276334971,
        "content": "My new task"
    }, {
        "id": 549277506,
        "content": "Douplicatee"
    }]
}, {
    "field_key": "progress",
    "field_type": "progress",
    "field_label": "Progress",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": 100
    }, {
        "id": 365047528,
        "content": 100
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": 100
    }, {
        "id": 485226870,
        "content": null
    }, {
        "id": 276334971,
        "content": 60
    }, {
        "id": 549277506,
        "content": 0
    }]
}, {
    "field_key": "ends_at_date",
    "field_type": "date",
    "field_label": "Due date",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": null
    }, {
        "id": 365047528,
        "content": null
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": null
    }, {
        "id": 485226870,
        "content": "2020-08-25"
    }, {
        "id": 276334971,
        "content": "2020-08-16"
    }, {
        "id": 549277506,
        "content": null
    }]
}];

const array2 = [{
      key: 'title',
      label: 'Title',
      field: 'text',
      visible: true
    }, {
      key: 'ends_at_date',
      label: 'Due date',
      field: 'date',
      visible: true
    }, {
      key: 'progress',
      label: 'Progress',
      field: 'progress',
      visible: true
    }];
    
    
function sortByAppearance( array1, array2 ) {
  const dict = Object.assign({}, ...array2.map( (v, i) => ({ [v.key]: i }) ) );
  const result = Array(array1.length);
  for (let value of array1) {
    result[dict[value.field_key]] = value;
  }
  return result;
}

console.log( sortByAppearance( array1, array2 ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.