Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 类型脚本数组排序_Arrays_Angular_Typescript - Fatal编程技术网

Arrays 类型脚本数组排序

Arrays 类型脚本数组排序,arrays,angular,typescript,Arrays,Angular,Typescript,如何在Typescript中按字母顺序对对象数组排序。我现在有这种格式的数组- 我希望按频道字段的字母顺序对其进行排序 这是我从网上得到的我试过的,它没有显示任何错误,也没有产生预期的结果 this.currentPickSelection = this.currentPickSelection.sort((a, b) => { if (a.channel > b.channel) { return 1; } if (a.chan

如何在Typescript中按字母顺序对对象数组排序。我现在有这种格式的数组- 我希望按频道字段的字母顺序对其进行排序

这是我从网上得到的我试过的,它没有显示任何错误,也没有产生预期的结果

 this.currentPickSelection = this.currentPickSelection.sort((a, b) => {
      if (a.channel > b.channel) {
        return 1;
      }
      if (a.channel < b.channel) {
        return -1;
      }
      return 0;
this.currentPickSelection=this.currentPickSelection.sort((a,b)=>{
如果(a通道>b通道){
返回1;
}
如果(a通道
您基本上必须对对象数组进行排序

[
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    }
]
将JSON对象更改为对象数组

[
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    }
]
编写一个类似这样的可重用函数。(与您使用的逻辑相同。我添加了
toLowerCase()
part)

私有比较(a、b){
if(a.channel.toLowerCase()b.channel.toLowerCase()){
返回1;
}
返回0;
}
然后,假设arrayOfObjects变量名为
channelDetails


只需
this.channelDetails.sort(this.compare);
就可以做到这一点。

基本上需要对对象数组进行排序

[
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    }
]
将JSON对象更改为对象数组

[
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    },
    {
        "pickCode":"",
        "cbsCode": "",
        "channel": "",
        "logo": "",
        "tag": ""
    }
]
编写一个类似这样的可重用函数。(与您使用的逻辑相同。我添加了
toLowerCase()
part)

私有比较(a、b){
if(a.channel.toLowerCase()b.channel.toLowerCase()){
返回1;
}
返回0;
}
然后,假设arrayOfObjects变量名为
channelDetails


只要
this.channelDetails.sort(this.compare);
就可以做到这一点。

您有一个多维数组。您的
数组#sort
方法应该是

this.currentPickSelection.sort((a, b) => a[0].channel.localeCompare(b[0].channel))

您有一个多维数组。您的
array#sort
方法应该是

this.currentPickSelection.sort((a, b) => a[0].channel.localeCompare(b[0].channel))

你的
json
对象出错了为什么每个数组中都有一个数组?你必须更改json对象。可能是@Saiyaff的重复,我正在尝试这样做,但我不确定它是如何发生的。@Tsvetan Ganev这是我从中获得代码片段的链接。我无法解决这个问题,这就是我创建另一个的原因。你的
json
对象错误为什么每个数组中都有一个数组?你必须更改JSON对象。可能是@Saiyaff的重复,我正在尝试,但我不确定它是如何发生的。@Tsvetan Ganev这是我从中获得代码片段的链接。我无法解决该问题,这就是我创建另一个数组的原因。这很有效。谢谢@Saiyaff Farouk成功了。谢谢@Saiyaff Farouk