Skip to content
Advertisement

sort order should be by date, then alphabetical (so all active at top A-Z, then all inactive at bottom A-Z)

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

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement