skip to Main Content

I’m struggle to get array to sort the order but need to put null on top first then descend order for eg

var test = ["1998", "2005", null, "2015", "2022"]; 

I would like expect it to order in [null, "2022", "2015", "2005", "1998"];

What I’ve tried.

test.sort(function(a, b) {
  return (
    (b === null) - (a === null) ||
    ("" + b).localeCompare(a)
  );
});

the problem is I’m using typescript, as there was error:

The left-hand side of an arithmetic operation must be of type any, number, bigInt or an enum type.

The right-hand side of an arithmetic operation must be of type any, number, bigInt or an enum type.

How do I resolve the issue?

6

Answers


  1. Chosen as BEST ANSWER

    This Has solved the problem. Thank you. By the way you can change null to "" if there is blank space just in case.

    let test = ["1998", "2005", "", "2015", "", "", "2022", ""];
    test.sort((a, b) => {
      if (a === b) return 0;
      if (a === "") return -1;
      if (b === "") return 1;
      return b.localeCompare(a);
    });
    console.log(test);

    let test = ["1998", "2005", "", "2015", "", "", "2022", ""];
    test.sort((a, b) => {
      if (a === b) return 0;
      if (a === "") return -1;
      if (b === "") return 1;
      return b.localeCompare(a);
    });
    console.log(test);


  2. Instead of subtracting booleans, you can explicitly write out the conditions.

    let test = ["1998", "2005", null, "2015", null, null, "2022", null];
    test.sort((a, b) => {
      if (a === b) return 0;
      if (a === null) return -1;
      if (b === null) return 1;
      return b.localeCompare(a);
    });
    console.log(test);

    Alternatively, you can use the unary plus operator to coerce to a number.

    let test = ["1998", "2005", null, "2015", null, null, "2022", null];
    test.sort((a, b) => +(b === null) - +(a === null) || ("" + b).localeCompare("" + a));
    console.log(test);
    Login or Signup to reply.
  3. You can use sort() then follow by reverse() as follow

    var test = ["1998", "2005", null, "2015", "2022"];
    
    console.log(test.sort().reverse())
    

    which produces

    [ null, '2022', '2015', '2005', '1998' ]
    
    Login or Signup to reply.
  4. The TypeScript error

    The reason you get these errors is your attempt to subtract boolean by boolean which is invalid:

    const result = true - true
    //             ^^^^^^^^^^^
    //             The right-hand side of an arithmetic operation must be of
    //             type 'any', 'number', 'bigint' or an enum type.ts(2363)
    

    You must convert boolean to apply the operation. i.e. turn it into one of any, number, bigInt, or enum.

    Sort

    In this case, a boolean is converted to 0 | 1 by JavaScript. So, do it explicitly in TypeScript.

    test.sort(function(a, b) {
      return (
        +(b === null) - +(a === null) ||
        ("" + b).localeCompare(a)
      );
    });
    

    After this, you may still see an error:

    Type 'null' is not assignable to type 'string'.ts(2769)
    

    This is because the variable a has a type string | null but localeCompare need a string. In another word, ("" + b).localeCompare(a) only make scene if both a and b are of type string(in TypeScript). So just separate the null check:

    test.sort(function (a, b) {
      if (a === null) return -1 // [a, b]
      if (b === null) return 1  // [b, a]
      return b.localeCompare(a)
    })
    
    Login or Signup to reply.
  5. You could sort null to top and the rest by value.

    const
        test = ["1998", "2005", null, "2015", "2022"];
    
    test.sort((a, b) => (b === null) - (a === null) || b - a);
    
    console.log(test);
    Login or Signup to reply.
  6. You can use localeCompare and with ternary operator for shorter syntax

    const test = ["1998", "2005", null, "2015", "2022"];
    
    test.sort((a, b) => a === null ? -1 : b === null ? 1 : b.localeCompare(a));
    
    console.log(test); 
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search