在不同的变量中对数组typescript进行排序

时间:2018-05-02 00:32:33

标签: typescript sorting ionic-framework arrayobject

我正在使用Ionic框架,我有一个对象数组如下:

[{
    "label_gas": "calle 79",
    "latitud_gas": "4.6653863",
    "longitud_gas": "-74.057738",
    "direccion_gas": "carrera 15 79 - 05",
    "telefono_gas": "0",
    "info_gas": "lunes a viernes de 9am a 4 pm",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.887783486866033",
    "price": 8900,
    "timeorigin": 856,
    "distanceorigin": 4012,
    "timetext": "14 min",
    "disttext": "4,0 km"
}, {
    "label_gas": "carulla pepe sierra",
    "latitud_gas": "4.6631135",
    "longitud_gas": "-74.058953",
    "direccion_gas": "carrera 15 114 - 32 piso 2",
    "telefono_gas": "0",
    "info_gas": "lunes a viernes de 9am a 4 pm",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.914579482181539",
    "price": 8900,
    "timeorigin": 807,
    "distanceorigin": 3726,
    "timetext": "13 min",
    "disttext": "3,7 km"
}, {
    "label_gas": "metropolis",
    "latitud_gas": "4.6808656",
    "longitud_gas": "-74.082527",
    "direccion_gas": "carrera 68 75a - 50 local 201",
    "telefono_gas": "2259757",
    "info_gas": "lunes a viernes de 9 am a 7 pm sabados de 10 am a 4 pm",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "0.3627612769464051",
    "price": 8900,
    "timeorigin": 201,
    "distanceorigin": 722,
    "timetext": "3 min",
    "disttext": "0,7 km"
}, {
    "label_gas": "polo club  bogota",
    "latitud_gas": "4.66976027",
    "longitud_gas": "-74.063396",
    "direccion_gas": "CARRERA 24 N\u00b0 80 - 56",
    "telefono_gas": "7454692",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.102835544304587",
    "price": 8900,
    "timeorigin": 657,
    "distanceorigin": 4430,
    "timetext": "11 min",
    "disttext": "4,4 km"
}, {
    "label_gas": "las ferias  bogota",
    "latitud_gas": "4.68368472",
    "longitud_gas": "-74.091205",
    "direccion_gas": "AVENIDA CALLE 72 N\u00b0 69P - 15",
    "telefono_gas": "3114088",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "1.347357157524655",
    "price": 8900,
    "timeorigin": 421,
    "distanceorigin": 1927,
    "timetext": "7 min",
    "disttext": "1,9 km"
}, {
    "label_gas": "toberin  bogota",
    "latitud_gas": "4.66705583",
    "longitud_gas": "-74.060799",
    "direccion_gas": "AUTOPISTA NORTE #163 A 68",
    "telefono_gas": "7426684",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.5013177470053187",
    "price": 8900,
    "timeorigin": 723,
    "distanceorigin": 5189,
    "timetext": "12 min",
    "disttext": "5,2 km"
}, {
    "label_gas": "los alcazares  bogota",
    "latitud_gas": "4.66233277",
    "longitud_gas": "-74.066034",
    "direccion_gas": "CALLE 72 N\u00b0 22 - 42",
    "telefono_gas": "7424219",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.3931383808296447",
    "price": 8900,
    "timeorigin": 652,
    "distanceorigin": 3133,
    "timetext": "11 min",
    "disttext": "3,1 km"
}, {
    "label_gas": "morato  bogota",
    "latitud_gas": "4.69900472",
    "longitud_gas": "-74.069650",
    "direccion_gas": "AVENIDA SUBA N\u00b0 115 - 79",
    "telefono_gas": "7431438",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.5503614606469958",
    "price": 8900,
    "timeorigin": 654,
    "distanceorigin": 3494,
    "timetext": "11 min",
    "disttext": "3,5 km"
}, {
    "label_gas": "autopista 98  bogota",
    "latitud_gas": "4.68513361",
    "longitud_gas": "-74.056775",
    "direccion_gas": "CALLE 97 N\u00b0 23 - 60",
    "telefono_gas": "6182360",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.6922032453221525",
    "price": 8900,
    "timeorigin": 665,
    "distanceorigin": 5019,
    "timetext": "11 min",
    "disttext": "5,0 km"
}, {
    "label_gas": "titan plaza  bogota",
    "latitud_gas": "4.69587583",
    "longitud_gas": "-74.087193",
    "direccion_gas": "CARRERA 72 N\u00b0 80-94 LOCAL 1- 1",
    "telefono_gas": "7451222",
    "info_gas": "l - v de 9 am a 3  pm = 26",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.0723238665716806",
    "price": 8900,
    "timeorigin": 731,
    "distanceorigin": 3867,
    "timetext": "12 min",
    "disttext": "3,9 km"
}, {
    "label_gas": "centro de pagos occidente  bogota",
    "latitud_gas": "4.68720861",
    "longitud_gas": "-74.081851",
    "direccion_gas": "AVENIDA CALLE 80 N\u00b0 69 - 40",
    "telefono_gas": "4395276",
    "info_gas": "l - v de 9 am a 7 pm = 10",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "0.9747749482713376",
    "price": 8900,
    "timeorigin": 651,
    "distanceorigin": 2849,
    "timetext": "11 min",
    "disttext": "2,8 km"
}, {
    "label_gas": "santa helenita  bogota",
    "latitud_gas": "4.65963638",
    "longitud_gas": "-74.062313",
    "direccion_gas": "Avenida calle 72 # 81A - 20\/26",
    "telefono_gas": "7431832",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.8920438525050627",
    "price": 8900,
    "timeorigin": 647,
    "distanceorigin": 3138,
    "timetext": "11 min",
    "disttext": "3,1 km"
}, {
    "label_gas": "la castellana  bogota",
    "latitud_gas": "4.68335361",
    "longitud_gas": "-74.062572",
    "direccion_gas": "Carrera 49B NO.94-52",
    "telefono_gas": "743",
    "info_gas": "l - v de 9 am a 5 pm = 17",
    "vlr_dtg": "8.900",
    "desc_param": "Gasolina",
    "distance": "2.020259659681773",
    "price": 8900,
    "timeorigin": 703,
    "distanceorigin": 4517,
    "timetext": "12 min",
    "disttext": "4,5 km"
}, {
    "label_gas": "calle 79",
    "latitud_gas": "4.6653863",
    "longitud_gas": "-74.057738",
    "direccion_gas": "carrera 15 79 - 05",
    "telefono_gas": "0",
    "info_gas": "lunes a viernes de 9am a 4 pm",
    "vlr_dtg": "9.000",
    "desc_param": "ACPM",
    "distance": "2.887783486866033",
    "price": 9000,
    "timeorigin": 856,
    "distanceorigin": 4012,
    "timetext": "14 min",
    "disttext": "4,0 km"
}]

我将变量声明如下:

jsonDataGas: any;
sortJsonDataPrice: any;
sortJsonDataDistance: any;

在构造函数中我需要填充每个变量(sortJsonDataPrice,sortJsonDataDistance),对变量jsonDataGas中的主数组进行排序

constructor(public navCtrl: NavController, public navParams: NavParams, private superTabsCtrl: SuperTabsController) {
    this.jsonDataGas = navParams.get('jsonDataGas');
    console.log(this.jsonDataGas);
    this.sortJsonDataPrice = this.jsonDataGas.sort((a,b) => {
      let nameA=a.label_gas.toLowerCase(), nameB=b.label_gas.toLowerCase()
      return a.price-b.price || nameA < nameB;
    });
    console.log(this.sortJsonDataPrice);
    this.sortJsonDataDistance = this.jsonDataGas.sort((a,b) => {
      return a.distanceorigin-b.distanceorigin;
    });
    console.log(this.sortJsonDataDistance);
    console.log(this.jsonDataGas);
  }

当打印变量时,它们都保持相同的顺序,主变量jsonDataGas被修改。

我没有使用过此框架,并希望得到任何帮助。

1 个答案:

答案 0 :(得分:1)

JavaScript Array.prototype.sort就地排序。也就是说,它会修改它正在运行的阵列。您第一次拨打this.jsonDataGas.sort以特定方式对this.jsonDataGas进行排序,然后您的第二次通话以另一种方式对其进行排序。

由于您需要两个独立排序的数组,因此首先需要copy它们。试试this.jsonDataGas.slice().sort(...),其中slice为您提供阵列的浅层克隆。请注意,由于克隆很浅,因此它不会复制基础对象。因此,如果您执行this.sortJsonDataPrice[0].timetext = 'bla',那么也会修改this.sortJsonDataDistance(和this.jsonDataGas)中的某些元素。