angular - angular2 typescript function to sort an array of objects by multiple fields -


i trying convert this function angular2/typescript in order sort array of objects multiple fields.

i new typescript, can please me check it's clean?

i calling

prodatatemp.sort(this.utilsservice.sort_by('brand', 'name', 'model')); 

and in utils-service.ts:

sort_by(...args: any[]) { //multiple sort https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields         var fields = [],             n_fields = args.length,             field, name, reverse, cmp;          var default_cmp = function(a, b) {             if (a == b) return 0;             return < b ? -1 : 1;         };          var getcmpfunc = function(primer, reverse) {             var dfc = default_cmp, // closer in scope                 cmp = default_cmp;             if (primer) {                 cmp = function(a, b) {                     return dfc(primer(a), primer(b));                 };             }             if (reverse) {                 return function(a, b) {                     return -1 * cmp(a, b);                 };             }             return cmp;         };          // preprocess sorting options         (var = 0; < n_fields; i++) {             field = args[i];             if (typeof field === 'string') {                 name = field;                 cmp = default_cmp;             }             else {                 name = field.name;                 cmp = getcmpfunc(field.primer, field.reverse);             }             fields.push({                 name: name,                 cmp: cmp             });         }          // final comparison function         return function(a, b) {             var a, b, name, result;             (var = 0; < n_fields; i++) {                 result = 0;                 field = fields[i];                 name = field.name;                  result = field.cmp(a[name], b[name]);                 if (result !== 0) break;             }             return result;         }     }; 

thanks !

function sortbymutiplefields(...args: string[]) {   // takes list of arguments representing sort fields   // if preceded ! reverse order (!lastname example)   // returns function works in array.sort()   return (a: any, b: any): number => {     (var index = 0; index < args.length; index++) {       let fieldleft: string;       let fieldright: string;       if (args[index].startswith('!')) {         fieldleft = b[args[index].slice(1)];         fieldright = a[args[index].slice(1)];       } else {         fieldleft = a[args[index]];         fieldright = b[args[index]];       }       if (fieldleft > fieldright) { return 1 };       if (fieldleft < fieldright) { return -1 };     };     return 0;   } } 

what doing way?


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -