0

Image
i have to show the list in sorting order with respect to signature date, if signature date is not past dated and if there is no date for signature date then it is considered as active record(sort order should be by status, then alphabetical (so all active at top A-Z, then all inactive at bottom A-Z). I have done sorting based on name but not able to push inactive signature dates to the end. I have attached DEMO as well.

const  employee = [
{
  name: 'jpat',
  signatureDate: '',
  businessType: 12346,
  originalFileName: 'hello.xls',
  agentW9id: 11,
  fileName: 'hello.xls',
  agentCode: 0,
  class: '',
  status: '',
},
{
  name: 'jcar',
  signatureDate: '09/10/2021',
  businessType: 12346,
  originalFileName: 'test.xls',
  agentW9id: 12,
  fileName: 'test.xls',
  agentCode: 0,
  class: '',
  status: '',
},
{
  name: 'Test',
  signatureDate: '09/23/2020',
  businessType: 12346,
  originalFileName: 'test.xls',
  agentW9id: 13,
  fileName: 'test.xls',
  agentCode: 0,
  class: 'inactive',
  status: 'Inactive',
},
{
  name: 'newTest',
  signatureDate: '10/9/2020',
  businessType: 12346,
  originalFileName: 'test.xls',
  agentW9id: 13,
  fileName: 'test.xls',
  agentCode: 0,
  class: 'inactive',
  status: 'Inactive',
},
{
  name: 'abc',
  signatureDate: '10/29/2021',
  businessType: 12346,
  originalFileName: 'test.xls',
  agentW9id: 13,
  fileName: 'test.xls',
  agentCode: 0,
  class: '',
  status: '',
},
{
  name: 'djhfj',
  signatureDate: '',
  businessType: 12346,
  originalFileName: 'test.xls',
  agentW9id: 13,
  fileName: 'test.xls',
  agentCode: 0,
  class: '',
  status: '',
},
  ];
  
console.log(employee.sort((a, b) => (a.name > b.name ? 1 : -1)));

DEMO

Bhrungarajni
  • 2,415
  • 11
  • 44
  • 88
  • 1
    Does this answer your question? [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – CBroe Sep 15 '21 at 08:57
  • @CBroe thanks for response, but no that is not helping me, i need to inactive status to go at bottom and empty status must be sorted with(A-Z). – Bhrungarajni Sep 20 '21 at 05:08

1 Answers1

1

Okay, updated code,

  1. break into objects based on status ( active and inactive)
  2. Sort each based on name and append back together

Working code below and Sandbox

Reult image enter image description here

const employee = [{
    name: 'jpat',
    signatureDate: '',
    businessType: 12346,
    originalFileName: 'hello.xls',
    agentW9id: 11,
    fileName: 'hello.xls',
    agentCode: 0,
    class: '',
    status: '',
  },
  {
    name: 'jcar',
    signatureDate: '09/10/2021',
    businessType: 12346,
    originalFileName: 'test.xls',
    agentW9id: 12,
    fileName: 'test.xls',
    agentCode: 0,
    class: '',
    status: '',
  },
  {
    name: 'Test',
    signatureDate: '09/23/2020',
    businessType: 12346,
    originalFileName: 'test.xls',
    agentW9id: 13,
    fileName: 'test.xls',
    agentCode: 0,
    class: 'inactive',
    status: 'Inactive',
  },
  {
    name: 'newTest',
    signatureDate: '10/9/2020',
    businessType: 12346,
    originalFileName: 'test.xls',
    agentW9id: 13,
    fileName: 'test.xls',
    agentCode: 0,
    class: 'inactive',
    status: 'Inactive',
  },
  {
    name: 'abc',
    signatureDate: '10/29/2021',
    businessType: 12346,
    originalFileName: 'test.xls',
    agentW9id: 13,
    fileName: 'test.xls',
    agentCode: 0,
    class: 'inactive',
    status: 'Inactive',
  },
  {
    name: 'djhfj',
    signatureDate: '',
    businessType: 12346,
    originalFileName: 'test.xls',
    agentW9id: 13,
    fileName: 'test.xls',
    agentCode: 0,
    class: '',
    status: '',
  }
];

var inactive = [],
  active = [];
employee.forEach((item) => {
  if (item.status == 'Inactive')
    inactive.push(item);
  else
    active.push(item);
});



inactive.sort(function(a, b) {
  if (a.name.toLowerCase() < b.name.toLowerCase()) {
    return -1;
  }
  if (a.name.toLowerCase() > b.name.toLowerCase()) {
    return 1;
  }
  return 0;
})

active.sort(function(a, b) {
  if (a.name.toLowerCase() < b.name.toLowerCase()) {
    return -1;
  }
  if (a.name.toLowerCase() > b.name.toLowerCase()) {
    return 1;
  }
  return 0;
})


var sortedArray = active.concat(inactive);

console.log(sortedArray);
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • Thanks for response, yes i tried but it is not working as expected, even though name abc must come at the top based on empty status or class sorting, that still comes in the middle – Bhrungarajni Sep 20 '21 at 05:21
  • How come? I have updated the code and its coming at top. check and run the snippet. I have named two names as abc for active and inactive both for you to see that they are sorted by class and names – Tushar Gupta Sep 20 '21 at 05:23
  • i am now able to see list of items i have added initially for result. i will paste the image how i am able to see – Bhrungarajni Sep 20 '21 at 05:26
  • i have attached image for the grid it is displaying – Bhrungarajni Sep 20 '21 at 05:27
  • @Bhrungarajni I have updated the answer and forked your code here https://stackblitz.com/edit/github-ck3u8u-zyoeqe?file=src%2Fapp%2Fuser-table%2Fuser-table.component.ts – Tushar Gupta Sep 20 '21 at 05:38
  • Thanks for response, but here if that abc is inactive class then that comes at the top, that is wrong as per my requirement – Bhrungarajni Sep 20 '21 at 05:52
  • my requirement is that, we need to display all active things at the top and inactive items at the last or bottom, and with this condition need to show active items sorted at top(A-Z) and inactive items at bottom sorted as (A-Z) – Bhrungarajni Sep 20 '21 at 05:57
  • @Bhrungarajni Cool, updated the code and sandbox: https://stackblitz.com/edit/github-ck3u8u-zyoeqe?file=src%2Fapp%2Fuser-table%2Fuser-table.component.ts – Tushar Gupta Sep 20 '21 at 06:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237271/discussion-between-tushar-and-bhrungarajni). – Tushar Gupta Sep 20 '21 at 06:25
  • 1
    Thanks a ton, working great and i have accepted your answer – Bhrungarajni Sep 20 '21 at 06:29