skip to Main Content

I have an array with following type of objects:

myArray = [
{state: "enabled", name: "zName"},
{state: "trying", name: "aName2"},
{state: "disabled", name: "aName3"},
{state: "disabled", name: "cName"},
{state: "disabled", name: "bName"}
]

I’m trying to sort the array in a way that it can be sort by:

  1. state == $givenValue
  2. name in ascending order of items that meet 1

As an example, if I wants to sort by state == "disabled", I’m expecting the array to sort as:

myArray = [
    {state: "disabled", name: "aName3"},
    {state: "disabled", name: "bName"},
    {state: "disabled", name: "cName"},
    {state: "trying", name: "aName2"},
    {state: "enabled", name: "zName"}
]

Unfortunately, I’m having trouble to come-up with a solution by myself. Can you help (?)

4

Answers


  1. To sort the array based on the given criteria (state == "disabled" and name in ascending order for items that meet the first criteria), you can use the Array.prototype.sort()

    const myArray = [
      {state: "enabled", name: "zName"},
      {state: "trying", name: "aName2"},
      {state: "disabled", name: "aName3"},
      {state: "disabled", name: "cName"},
      {state: "disabled", name: "bName"}
    ];
    const givenValue = "disabled";
    function customSort(a, b) {
      if (a.state === givenValue && b.state === givenValue) {
        return a.name.localeCompare(b.name);
      } else if (a.state === givenValue) {
        return -1;
      } else if (b.state === givenValue) {
        return 1;
      } else {
        return 0;
      }
    }
    myArray.sort(customSort);
    console.log(myArray);
    Login or Signup to reply.
  2. Yes, I can help you with that! You can use the Array.prototype.sort() method in JavaScript to sort the array based on the given criteria. Here’s an example of how you can achieve the desired sorting:

    const myArray = [
      { state: "enabled", name: "zName" },
      { state: "trying", name: "aName2" },
      { state: "disabled", name: "aName3" },
      { state: "disabled", name: "cName" },
      { state: "disabled", name: "bName" }
    ];
    
    const givenValue = "disabled";
    
    myArray.sort((a, b) => {
      if (a.state === givenValue && b.state !== givenValue) {
        return -1;
      } else if (a.state !== givenValue && b.state === givenValue) {
        return 1;
      } else {
        if (a.name < b.name) {
          return -1; 
        } else if (a.name > b.name) {
          return 1; 
        } else {
          return 0;
        }
      }
    });
    
    console.log(myArray);
    Login or Signup to reply.
  3. 
    const myArray = [
      {state: "enabled", name: "zName"},
      {state: "trying", name: "aName2"},
      {state: "disabled", name: "aName3"},
      {state: "disabled", name: "cName"},
      {state: "disabled", name: "bName"}
    ];
    
    const givenValue = "disabled";
    
    myArray.sort((a, b) => {
      if (a.state === givenValue && b.state !== givenValue) {
        return -1; // 'a' comes before 'b'
      } else if (a.state !== givenValue && b.state === givenValue) {
        return 1; // 'b' comes before 'a'
      } else {
        // If both have the same state or both don't have the given state,
        // sort them based on the 'name' property in ascending order.
        return a.name.localeCompare(b.name);
      }
    });
    
    console.log(myArray);```
    
    
    Certainly! I can help you with that. To sort the array according to the given conditions, you can use the JavaScript Array.prototype.sort() method with a custom sorting function. Here's an example code snippet that achieves the desired sorting:
    
    javascript
    Copy code
    const myArray = [
      {state: "enabled", name: "zName"},
      {state: "trying", name: "aName2"},
      {state: "disabled", name: "aName3"},
      {state: "disabled", name: "cName"},
      {state: "disabled", name: "bName"}
    ];
    
    const givenValue = "disabled";
    
    myArray.sort((a, b) => {
      if (a.state === givenValue && b.state !== givenValue) {
        return -1; // 'a' comes before 'b'
      } else if (a.state !== givenValue && b.state === givenValue) {
        return 1; // 'b' comes before 'a'
      } else {
        // If both have the same state or both don't have the given state,
        // sort them based on the 'name' property in ascending order.
        return a.name.localeCompare(b.name);
      }
    });```
    In this code, the sort() method is used with a custom comparison function. It first checks if the state property of both objects (a and b) matches the givenValue. If one object has the givenValue and the other doesn't, the one with the givenValue is placed before the other.
    
    If both objects have the same state or both don't have the givenValue, the name property is compared using the localeCompare() method to achieve ascending order sorting.
    
    The resulting sorted array will be printed in the console.
    
    Running the above code will give you the following output:
    
    Login or Signup to reply.
  4. I would create an array to define the order ["enabled", "trying", "disabled"] and by the object value get the index from the array, so like you can sort them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search