Object arrays are sorted according to Boolean values true and false - js basic accumulation

Application scenarios:

Recently, when I was writing a backend management system, I encountered a requirement, that is, in an object array, a certain field has a Boolean value, and it needs to be sorted based on this Boolean value. True should be placed in the front, false should be placed in the back.

The renderings are as follows:

Insert image description here

The parameters are as follows:

[
  {
    
    
       "code": "1",
       "name": "客诉",
       "responseHours": 24,
       "defaultExecutors": [
           {
    
    
               "workOrderTypeCode": "1",
               "executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
               "executorName": "admin",
               "jobPositionCode": "backendDevelopment",
               "jobPosition": "后端开发",
               "jobDescription": "333",
               "isMainExecutor": true,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "ba30a026-9ce5-7760-f37f-3a0f4b4ab3e6"
           }
       ],
       "id": "5c86ead8-69f6-8525-4b02-3a0f0dad5e75"
   },
   {
    
    
       "code": "2",
       "name": "悦捷",
       "responseHours": 24,
       "defaultExecutors": [
           {
    
    
               "workOrderTypeCode": "2",
               "executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
               "executorName": "admin",
               "jobPositionCode": "frontEndDevelopment",
               "jobPosition": "前端开发",
               "jobDescription": "333",
               "isMainExecutor": true,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "03be553c-f568-b1f3-2d8f-3a0f4b4aa0f9"
           }
       ],
       "id": "5823da4d-6d90-8d21-f1cc-3a0f0db0413b"
   },
   {
    
    
       "code": "3",
       "name": "下单异常",
       "responseHours": 2,
       "defaultExecutors": [
           {
    
    
               "workOrderTypeCode": "3",
               "executorId": "0859c01b-d7a1-0830-a02c-3a0f1170134a",
               "executorName": "ceshi",
               "jobPositionCode": "frontEndDevelopment",
               "jobPosition": "前端开发",
               "jobDescription": "职责2",
               "isMainExecutor": false,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "b4c8f343-ce29-f891-8993-3a0f53f63211"
           },
           {
    
    
               "workOrderTypeCode": "3",
               "executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
               "executorName": "admin",
               "jobPositionCode": "backendDevelopment",
               "jobPosition": "后端开发",
               "jobDescription": "职责",
               "isMainExecutor": false,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "8b67f701-9edd-d0e1-bf92-3a0f53f63211"
           }
       ],
       "id": "611446a7-b1d1-417b-6002-3a0f53f6320d"
   },
   {
    
    
       "code": "2444",
       "name": "3",
       "responseHours": 4,
       "defaultExecutors": [
           {
    
    
               "workOrderTypeCode": "2444",
               "executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
               "executorName": "admin",
               "jobPositionCode": "backendDevelopment",
               "jobPosition": "后端开发",
               "jobDescription": "33333",
               "isMainExecutor": false,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "e4f19a49-b7e1-876e-5d75-3a0f54101520"
           },
           {
    
    
               "workOrderTypeCode": "2444",
               "executorId": "0859c01b-d7a1-0830-a02c-3a0f1170134a",
               "executorName": "ceshi",
               "jobPositionCode": "frontEndDevelopment",
               "jobPosition": "前端开发",
               "jobDescription": "44444",
               "isMainExecutor": true,
               "creationTime": "0001-01-01T00:00:00",
               "creatorId": null,
               "id": "5fbb17f5-0de6-f432-9d82-3a0f54101520"
           }
       ],
       "id": "11f111d0-db69-7e1c-f65b-3a0f54101517"
   }
]

should be performed according to in the defaultExecutors field in the object array and sorting. isMainExecutortruefalse

Sort by boolean value

//下面的res是接口返回的格式,items是上面的对象数组
let tableData = res.items || [];
tableData.forEach((tab) => {
    
    
  if (tab.defaultExecutors) {
    
    
    tab.defaultExecutors = tab.defaultExecutors.sort(
      (a, b) => b.isMainExecutor - a.isMainExecutor
    );
  }
});
this.tableData = [...tableData];

Summarize:

1. Put the true ones first - count in descending order

tab.defaultExecutors = tab.defaultExecutors.sort(
  (a, b) => b.isMainExecutor - a.isMainExecutor
);

2. Put the true ones at the back - sort them in ascending order

tab.defaultExecutors = tab.defaultExecutors.sort(
  (a, b) => a.isMainExecutor - b.isMainExecutor
);

Finish! ! ! Accumulate a lot and gain a lot! ! !

Guess you like

Origin blog.csdn.net/yehaocheng520/article/details/134848070