Arrange in order by date followed by alphabetical sequence, ensuring that all active items are placed at the top in alphabetical order and all inactive items at the bottom in alphabetical order

https://i.sstatic.net/SgDDc.png
I need to display a list sorted by signature date. If the signature date is not past-dated or missing, the record is considered active and should be sorted first alphabetically. Inactive records should come after active ones in alphabetical order. I have managed to sort based on name but struggling to push inactive dates to the end. Check out the attached DEMO for reference.

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

Answer №1

After making some updates to the code:

  1. Separate objects based on their status - active and inactive
  2. Sort each group by name and combine them back together

Below is the revised code snippet and Sandbox Demo

Result screenshot: https://i.sstatic.net/Ayz2K.png

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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to define the width of an element within the display:flex property in CSS?

Could you please review the following: https://codepen.io/sir-j/pen/dyRVrPb I am encountering an issue where setting the input [type=range] to 500px doesn't seem to make a difference from 100px, 500px, or 800px. This leads me to believe that display ...

Is there a way to split each foreach value into distinct variables?

I am looking to assign different variables to foreach values. I have fetched data from an API in JSON format, and then echoed those values using a foreach loop. My goal is to display the echoed value in an input box using JavaScript. I attempted the follow ...

Storing a selected database column as a variable from an HTML page in Node.js with Express

My website showcases flight information pulled from a MySQL database. The process begins with a form where users select destinations and dates for their desired flight. Once the form is submitted, users are directed to another page where flights matching t ...

Difficulties with managing button events in a Vue project

Just getting started with Vue and I'm trying to set up a simple callback function for button clicks. The callback is working, but the name of the button that was clicked keeps showing as "undefined." Here's my HTML code: <button class="w ...

Adjust the text appearance of the graph in ApexCharts

Is there a way to adjust the font size of the donut chart generated by using apexchart? You can view the image here. <template> <section> <v-card raised> <v-card-title class="blue--text">Requests Overview</v-card-ti ...

Using Jquery and Ajax to pass an extra PHP variable to a server-side script

I am working with a dropdown select box where the selected option is sent to a server-side script using Ajax. <select id="main_select"> <option selected="selected" value="50">50</option> <option ...

How can an object inside an array be destructured in just one line?

Consider the following array: const array = [{b:2}] Can you extract the value of b using destructuring in just one line? I attempted something similar to this approach, but it did not yield the desired result: const [{b} = array] ...

Discovering a particular element involves iterating through the results returned by the findElements method in JavaScript

I am attempting to locate and interact with a specific item by comparing text from a list of items. The element distinguished by .list_of_items is a ul that consists of a list of li>a elements. I am uncertain about how to transfer the determined elemen ...

Despite adding app.use(flash()) to resolve the issue, the error 'TypeError: req.flash is not a function' persists

I encountered an error in my routes.js file: app.get('/login', function(req, res) { // Display the page and include any flash data if available res.render('login.html', { message: req.flash('loginMessage') }); }); ...

executing a JavaScript function in a separate .js document

When working with the success callback function in my AJAX post, I encountered an issue trying to call a function from another JavaScript file. Within page1.html: <head> <link href="style.css" rel="stylesheet" type="text/css" /> <s ...

What are the steps for combining two constants with the JSON format of "const = [ { } ]"?

Here is the initial code that I would like to merge with another section. const sections = [ { title: section1_title, rows: [{ title: section1_option01, rowId: "sec1_option01", ...

Text input setting for jQuery UI Slider

Currently, I am utilizing jQuery UI sliders to input values in text boxes. However, I would like this functionality to be bidirectional; meaning if a value is entered into the text box, I want the slider to move to the corresponding position. I am unsure ...

Utilizing JavaScript/AJAX JSON encoding for arrays: Tips for effectively utilizing the received data

I am trying to retrieve all the images from a specific folder on the server using an AJAX call. files = glob('assets/img/daily/*'); // obtain all file names $imageArr; foreach ($files as $file) { $imageArr[] = $file; } $jsonObj = json_encode ...

Dynamically incorporating parameters into a function without any limitations

Is there a way to dynamically pass multiple arrays as function parameters and then store their values into a single array? I already know how to accomplish the second part, as shown below: function arrayfunction(/*arrays go here*/) { var allArrays = [] ...

Is it possible to target elements within a UMAP iframe using CSS?

I have integrated a uMap map into my website. Here is the code: <iframe id="umapiframe" class="iframe-umap" width="100%" height="300px" frameborder="0" allowfullscreen src="//umap.openstreetmap.fr/f ...

Creating smooth and natural movement of a div element using Javascript (rotating and moving)

After struggling to use jquery plugins for smooth motion with the div elements I'm working on, I've decided it's time to seek some assistance. I have a group of div elements that all share a class and I want them to move around the screen c ...

Using conditional rendering within the map function in React

I am working with the code snippet below and I am looking to implement a conditional rendering to exclude index 0 from being displayed. How can I achieve this? return ( <section> {pokemonCards.map((pokemon, index) => ...

Passport.socket.io cannot resolve the issue of a missing session

The Issue I am facing a problem with the failed connection to socket.io: No session found using passport.socketio.js and I am unable to identify the root cause. Despite checking similar posts, the configuration seems fine to me. However, it appears that ...

What steps can be taken to delete a cookie when the server cannot be accessed?

Currently utilizing a node.js + express server and managing sessions for users who are logged in. I am curious about how a user can log out if the server becomes unreachable? In the usual method, I would simply call req.logout() on the request object in ...

How to locate the parent element in a JQuery event

I need to verify whether the target has a specific parent after adding a click event. This is how my code looks: $(document).click(function(event){ // Check here if the target has a specific parent, for example -> #parent }); Is there a way to ach ...