代码之家  ›  专栏  ›  技术社区  ›  santosh kumar patro

使用typescript对数组进行排序,但有一项除外

  •  0
  • santosh kumar patro  · 技术社区  · 7 年前

    我有以下类型脚本代码:

    export const cgGroups = [
      {
        id: 2,
        name: 'North America & Caribbean'
      },
      {
        id: 3,
        name: 'Latin America'
      },
      {
        id: 6,
        name: 'Europe'
      },
      {
        id: 4,
        name: 'Asia Pacific'
      },
      {
        id: 1,
        name: 'Middle East & Africa'
      },
      {
        id: 7,
        name: 'International'
      }
    ];
    

    我想按字母顺序对上面的内容进行排序,除了一个对象

    {
       id: 7,
       name: 'International'
    }
    

    我想把它移到排序数组的最后一个。

    我尝试了以下代码进行排序:

    cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
            if (a.name.toLowerCase() > b.name.toLowerCase()) {
              return 1;
            }
            if (a.name.toLowerCase() < b.name.toLowerCase()) {
              return -1;
            }
            return 0;
          });
    

    预期产量如下:

    亚太、欧洲、拉丁美洲、中东和非洲、北美和加勒比以及国际

    有人能指导我来解决这个问题吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   bugs    7 年前

    如果您不介意向数组原型添加方法,那么这是两个可能的解决方案(第一个解决方案修改原始数组,而第二个解决方案返回新数组)。

    let cgGroups = [
        {
            id: 2,
            name: 'North America & Caribbean'
        },
        {
            id: 3,
            name: 'Latin America'
        },
        {
            id: 6,
            name: 'Europe'
        },
        {
            id: 4,
            name: 'Asia Pacific'
        },
        {
            id: 1,
            name: 'Middle East & Africa'
        },
        {
            id: 7,
            name: 'International'
        }
    ];
    
    const sortAlph = (a, b) => {
        if (a.name.toLowerCase() > b.name.toLowerCase()) {
            return 1;
        }
        if (a.name.toLowerCase() < b.name.toLowerCase()) {
            return -1;
        }
        return 0;
    }
    
    Array.prototype.move = function (fromIndex, toIndex) {
      let element = this[fromIndex];
      this.splice(fromIndex, 1);
      this.splice(toIndex, 0, element);
    }
    
    Array.prototype.moveToTheEnd = function(index) {
      let element = this[index];
      return this.filter(x => x !== element).concat(element);
    }
    
    cgGroups
        .sort(sortAlph)
        .move(cgGroups.findIndex(x => x.name === 'International'), cgGroups.length)
    
    newArr = cgGroups
      .sort(sortAlph)
      .moveToTheEnd(cgGroups.findIndex(x => x.name === 'International'))
    
    console.log(cgGroups);
    console.log(newArr);
        2
  •  2
  •   axiac    7 年前

    它不起作用,因为您没有用 name: 'International' 进入比较函数。

    可能是这样的:

    cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
        if (a.name.toLowerCase() == 'international') {
            return +1;      // "a" is the greatest element of the array
        } else if (b.name.toLowerCase() == 'international') {
            return -1;      // "a" stays before "b" because "b" is the last item
        } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
            return 1;       // regular items, compare their names
        } else if (a.name.toLowerCase() < b.name.toLowerCase()) {
            return -1;
        }
    
        return 0;
    });
    
    推荐文章