Use Typescript to array [{'a':'','b':'s'},{'a':'b','b':''},{"a":'s','b ':'s'}] Sort by the value of a. If the values of a are the same, sort by the value of b.

Use Typescript to array [{'a':'','b':'s'},{'a':'b','b':''},{"a":'s','b ':'s'}] Sort by the value of a. If the values ​​of a are the same, sort by the value of b.

You can sort an array using the Array.sort() method. The sort() method accepts a comparison function as argument, which sorts the array elements according to specified rules.

Here is a code example to sort an array:

const arr = [{
    
    'a':'','b':'s'},{
    
    'a':'b','b':''},{
    
    "a":'s','b':'s'}];

arr.sort((a, b) => {
    
    
  if (a.a < b.a) {
    
    
    return -1;
  }
  if (a.a > b.a) {
    
    
    return 1;
  }
  if (a.b < b.b) {
    
    
    return -1;
  }
  if (a.b > b.b) {
    
    
    return 1;
  }
  return 0;
});

console.log(arr);

Guess you like

Origin blog.csdn.net/qq_39962271/article/details/129023085