skip to Main Content

I would like to know how to shift based on field that contains numeric string and normal string.

so, if numeric string value then shift to end of array, rest remain same

Tried
 const sortData = arrobj.sort((a: any, b: any) => {
      const sortA = a.cn;
      const sortB = b.cn;
      
      if(!isNaN(sortA)) {
        return -1;
      }
      if(!isNaN(sortB)) {
        return 1;
      }
      return sortA.localeCompare(sortB);
    });
    console.log("sortData", sortData);
var arrobj=[
  {cn: '99'}
  {cn: 'SG'},
  {cn: 'IN'}
]

expecteded output
[
  {cn: 'SG'},
  {cn: 'IN'},
  {cn: '99'}
]

2

Answers


  1. Hmm… I think that you just need to add extra conditions to your ifs…

    const arrobj =
    [
      {cn: '99'},
      {cn: 'SG'},
      {cn: 'IN'},
    ];
    
    const sortData = arrobj.sort
    (
      ( $1, $2 ) =>
      {
        const $1first = -1;
        const $2first = 1;
        const $equal = 0;
        
        if ( isNaN( parseFloat($1.cn) ) && ! isNaN( parseFloat($2.cn) ) )
        {
          // Only $1.cn is non-numeric. So, $2.cn is numeric.
          return $1first;
        }
        
        if ( isNaN( parseFloat($2.cn) ) && ! isNaN( parseFloat($1.cn) ) )
        {
          // Only $2.cn is non-numeric. So, $1.cn is numeric.
          return $2first;
        }
        
        // As requested, there's no sorting between both numbers or both strings...
        return $equal;
        // Remove the line above if you wish to order like below:
        
        // But you can sort them separatly... However you like...
        
        if ( isNaN( parseFloat($1.cn) ) && isNaN( parseFloat($2.cn) ) )
        {
          // Both $1.cn and $2.cn are non-numeric. Ascending order.
          return String($1.cn).localeCompare($2.cn);
        }
        
        else
        {
          // Both $1.cn and $2.cn are numeric. Descending order.
          return String($1.cn).localeCompare($2.cn) * -1;
        }
      }
    );
    console.log("sortData =", sortData);
    Login or Signup to reply.
  2. The OP had many syntactical errors, get in the habit of using a tool like JSHint. The order you want is just in reverse so use .reverse() Array method on the array after it’s sorted or just switch the parameters of .localeCompre(). Also, .localeCompare() doesn’t handle numbers correctly, so I updated to reflect that. Details are commented in example.

    Using .reverse()

    const sortData = (arr) => {
      // Define an array for strings and numbers
      let str = []; num = [];
      // Add strings to str and numbers to num
      arr.forEach(chr => {
        if (isNaN(chr.cn)) {
          str.push(chr);
        } else {
          num.push(chr);
        }
      });
      // Sort strings with localCompare() (reverse params b then a)
      str = str.sort((a, b) => b.cn.localeCompare(a.cn)); //🟂
      // Sort numbers by whether the difference is negative or positive 
      num = num.sort((a, b) => b.cn - a.cn); //🟆
      // Merge str and num into one array
      return [...str, ...num]; //🟊
      /**
       * Alternative using .reverse() change the denoted lines:
       *   🟂 a.cn.localeCompare(b.cn)
       *   🟆 a.cn - b.cn
       *   🟊 [...str, ...num].reverse()
       */   
    };
    
    const arrObj = [{cn: '99'}, {cn: 'SG'}, {cn: 'IN'}, {cn: '1'}, {cn: '666'}];
    
    console.log("sortData =", JSON.stringify(sortData(arrObj)));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search