msb onlinework
msb onlinework

Reputation: 1

Sorting Table nested hierarchy in every level

I have a table which has following mocked data

export const resultActionTable = {
  data: [
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer',
      marketLobIdent: 101,
      keyIssues: [
        {
          keyIssue:
            'Increase of escalation rate Todo esto lo estoy haciendo para probar cómo se comporta con todo el texto que le estoy metiendo.',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 101,
                    name: 'Not started',
                    bgColor: '#FFEAC5',
                    fgColor: '#C32813',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones', 'Stephanie Shark'],
                  isEditable: true,
                  statusAction: 'E',
                  impact: {
                    ident: 101,
                    key: 'gm',
                    name: 'GM',
                  },
                  source: {
                    ident: 101,
                    key: 'internalReview',
                    name: 'Internal Reviews',
                  },
                },
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 101,
                    name: 'Not started',
                    bgColor: '#FFEAC5',
                    fgColor: '#C32813',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones', 'Stephanie Shark'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 102,
                    key: 'revenue',
                    name: 'Revenue',
                  },
                  source: {
                    ident: 105,
                    key: 'tpLovesIdeas',
                    name: 'TP Loves Ideas',
                  },
                },
              ],
            },
            {
              rootCauseName: 'Lack of Knowledge on escalation process 2',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation 2',
                  actionIdent: 103,
                  status: {
                    ident: 102,
                    name: 'On hold',
                    bgColor: '#E7D6FF',
                    fgColor: '#780096',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
        {
          keyIssue:
            'This is another Key issue name that I just created to test the table with multiple key issues. :)',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName:
                'This is a new Root Cause I just added to test with examples. Lack of Knowledge on escalation process. This is extra text to have the root cause larger. :)',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 101,
                    name: 'Not started',
                    bgColor: '#FFEAC5',
                    fgColor: '#C32813',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones', 'Stephanie Shark'],
                  isEditable: true,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 101,
                    name: 'Not started',
                    bgColor: '#FFEAC5',
                    fgColor: '#C32813',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones', 'Stephanie Shark'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2024',
      marketLobIdent: 102,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 103,
                  status: {
                    ident: 102,
                    name: 'On hold',
                    bgColor: '#E7D6FF',
                    fgColor: '#780096',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Joshua Jones'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025',
      marketLobIdent: 103,
      keyIssues: [
        {
          keyIssue: 'Lack of Knowledge',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 105,
                    name: 'Closed successfully',
                    bgColor: '#B7FAC4',
                    fgColor: '#027650',
                  },
                  commentsCount: 0,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Stephanie Sharkey'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-II',
      marketLobIdent: 104,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 102,
                  status: {
                    ident: 105,
                    name: 'Closed successfully',
                    bgColor: '#B7FAC4',
                    fgColor: '#027650',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Alex Buckmaster'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-II',
      marketLobIdent: 105,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 101,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 102,
                  status: {
                    ident: 105,
                    name: 'Closed successfully',
                    bgColor: '#B7FAC4',
                    fgColor: '#027650',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Alex Buckmaster'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-III',
      marketLobIdent: 106,
      keyIssues: [
        {
          keyIssue: 'Lack of Knowledge',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 104,
                    name: 'Canceled',
                    bgColor: '#DDE4F8',
                    fgColor: '#575F8F',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Jerry Helfer'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-IV',
      marketLobIdent: 107,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 102,
                  status: {
                    ident: 103,
                    name: 'In progress',
                    bgColor: '#D7FDFF',
                    fgColor: '#307A7E',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Judith Rodriguez'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-V',
      marketLobIdent: 108,
      keyIssues: [
        {
          keyIssue: 'Lack of Knowledge',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 103,
                    name: 'In progress',
                    bgColor: '#D7FDFF',
                    fgColor: '#307A7E',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['David Elson'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-VI',
      marketLobIdent: 109,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 102,
                  status: {
                    ident: 102,
                    name: 'On hold',
                    bgColor: '#E7D6FF',
                    fgColor: '#780096',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Kenneth Allen'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-VII',
      marketLobIdent: 109,
      keyIssues: [
        {
          keyIssue: 'Lack of Knowledge',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Provide Training',
                  actionIdent: 102,
                  status: {
                    ident: 104,
                    name: 'Canceled',
                    bgColor: '#DDE4F8',
                    fgColor: '#575F8F',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Lorri Warf'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      marketLob: 'Vodafone Italia-Outbound-Business to Consumer 2025-VIII',
      marketLobIdent: 110,
      keyIssues: [
        {
          keyIssue: 'Increase of escalation rate',
          keyIssueIdent: 102,
          rootCauses: [
            {
              rootCauseName: 'Lack of Knowledge on escalation process',
              rootCauseIdent: 101,
              actions: [
                {
                  actionName: 'Conduct knowledge test on escalation',
                  actionIdent: 102,
                  status: {
                    ident: 106,
                    name: 'Closed unsuccessfully',
                    bgColor: '#FFD7DD',
                    fgColor: '#C70000',
                  },
                  commentsCount: 3,
                  createdDate: '2024-07-15',
                  originalDeadline: '2024-07-15',
                  currentDeadline: '2024-07-15',
                  actionOwner: ['Frances Swann'],
                  isEditable: false,
                  statusAction: 'E',
                  impact: {
                    ident: 103,
                    key: 'clientLoyalty',
                    name: 'Client Loyalty',
                  },
                  source: {
                    ident: 102,
                    key: 'leanSixSigmaTrigger',
                    name: 'Lean Six Sigma Trigger',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
  ],
};
 
 
 

So I want the table should sort in this manner where--

  1. Parent can able to sort its children but children not (e.q- marketLob can sort every its child field and keyIssue can do the same but it must not be able to sort its parent which is marketLob. And this process follows on
  2. In siblings sorting can be triggered.

I tried the following code but still not able to get what I exactly needed. Plz help

export function sortData(data: any[], orderKey: string, orderDirection: string): any[] {
  console.log(`sortData called with orderKey: ${orderKey}, orderDirection: ${orderDirection}`);
  if (!data.length) {
    console.warn('Data array is empty. Returning as is.');
    return data;
  }
 
  switch (orderKey) {
    case 'marketLob':
      data = data.sort((a, b) => {
        const valueA = a[orderKey];
        const valueB = b[orderKey];
        return compareValues(valueA, valueB, orderDirection);
      });
      break;
    default:
      data = data.sort((a, b) => {
        let valueA = a;
        let valueB = b;
 
        if (orderKey === 'keyIssue') {
          valueA = a.keyIssues[0][orderKey];
          valueB = b.keyIssues[0][orderKey];
        } else if (orderKey === 'rootCauseName') {
          valueA = a.keyIssues[0].rootCauses[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0][orderKey];
        } else if (orderKey === 'actionName') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0][orderKey];
        } else if (orderKey === 'actionSource') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0].source[orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0].source[orderKey];
        } else if (orderKey === 'primaryImpact') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0].impact.orderKey;
          valueB = b.keyIssues[0].rootCauses[0].actions[0].impact.orderKey;
        } else if (orderKey === 'actionOwner') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0][orderKey];
        } else if (orderKey === 'createdDate') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0][orderKey];
        } else if (orderKey === 'originalDeadline') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0][orderKey];
        } else if (orderKey === 'currentDeadline') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0][orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0][orderKey];
        } else if (orderKey === 'status') {
          valueA = a.keyIssues[0].rootCauses[0].actions[0].status[orderKey];
          valueB = b.keyIssues[0].rootCauses[0].actions[0].status[orderKey];
        }
 
        return compareValues(valueA, valueB, orderDirection);
      });
  }
 
  return data;
}
 
function compareValues(a: any, b: any, orderDirection: string): number {
  console.log(`Comparing values: a = ${a}, b = ${b}`);
 
  const aValue = a != null ? a.toString() : '';
  const bValue = b != null ? b.toString() : '';
 
  console.log(`Comparing values: aValue = ${aValue}, bValue = ${bValue}`);
 
  if (orderDirection === 'asc') {
    return aValue.localeCompare(bValue);
  } else {
    return bValue.localeCompare(aValue);
  }
}
 


Upvotes: -5

Views: 21

Answers (0)

Related Questions