0
import {Pipe, PipeTransform} from 'angular2/core';

@Pipe({  name: 'orderBy' })
export class OrderrByPipe implements PipeTransform {

  transform(records: Array<any>, args?: any): any {

    return records.sort(function(a, b){
          if(a[args.property] < b[args.property]){
            return -1 * args.direction;
          }
          else if( a[args.property] > b[args.property]){
            return 1 * args.direction;
          }
          else{
            return 0;
          }
        });
    };
}

Please check this, it worked for me, but i see one thing in that, integer sorting suppose if we take 1,2,3,4,5,6,7,8,9,10,11,12,13. it is behaving wierd

Haifeng Zhang
  • 24,348
  • 16
  • 55
  • 104
vikas
  • 9
  • 2

1 Answers1

0

The general problem faced in ordering an array is because of the datatypes used in an array.

As an example -

console.log(2 > 11) //result is false
console.log("2" > "11") //result is true

This is the reason why your pipe is working fine for numbers from 1 to 9 and failing after 9.

The comparison of the array element should therefore be handled considering the datatype of the items being compared.

EDIT - Sorting

Consider following example where string and number type comparisons are handled separately.

function orderByComparator(a, b) {
    if ((isNaN(parseFloat(a)) || !isFinite(a)) || (isNaN(parseFloat(b)) || !isFinite(b))) {
        //Isn't a number so lowercase the string to properly compare
        if (a.toLowerCase() < b.toLowerCase())
            return -1;
        if (a.toLowerCase() > b.toLowerCase())
            return 1;
    }
    else {
        //Parse strings as numbers to compare properly
        if (parseFloat(a) < parseFloat(b))
            return -1;
        if (parseFloat(a) > parseFloat(b))
            return 1;
    }
    return 0; //equal each other
}


console.log("number comparison - ", orderByComparator(2, 11));
console.log("string comparison - ", orderByComparator("2", "11"));

Example

this plunkr explains the comparison in details (check orderBy.ts pipe definition)

I hope this helps :)

planet_hunter
  • 3,425
  • 1
  • 19
  • 32
  • Hi, Its working fine for numeric but its throwing me an error lowercase not defined... here is the code return c.sort(function(a, b) { if ((isNaN(parseFloat(a[args.property])) || !isFinite(a[args.property])) || (isNaN(parseFloat(b[args.property])) || !isFinite(b[args.property]))) { //Isn't a number so lowercase the string to properly compare if (a[args.property].toLowerCase() < b[args.property].toLowerCase()) return -1 * args.direction; – vikas Jun 01 '18 at 06:34
  • Okay. Can you tell me what did you use as arguments for the function? It is working fine for me. – planet_hunter Jun 01 '18 at 17:42
  • this.data = this.data.sort((a, b) => new Date(a.JsonDate).getTime() - new Date(b.JsonDate).getTime()).reverse(); is working fine for values till 9, its breaking for values greater than 10, kindly help me how can i make it work for greator than 10, i tried using ">" sign but its not working as expected . – vikas Jun 25 '18 at 17:17
  • Looks like you want to order the array by date object. You can follow [this link](https://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) which explains the date time conversion. Let me know if you are looking for something different :) – planet_hunter Jun 25 '18 at 17:39