0

Good day,

How can i sort numeric value in angular 2 using pipes? or is their any builtin pipes for sorting numbers?

<div class="group group1">
<div *ngFor="let apidata of data">
  <div *ngIf="apidata.AssignmentNumber[0] == 1" class="box">
      <div class="box-assignmentnumber-holder">
        <span id="AssignmentNumber" [ngStyle]="{'color': apidata.AssignmentNumber[1] == 1 ? '#FF8C00' : 'green'}">{{apidata.AssignmentNumber | desc}}</span>
      </div>
      <div id="arrow" (click)="this.clickMe =! this.clickMe"></div>
  </div>
</div>

above is my code in displaying number, but i want to sort them in ascending order.

Jydon Mah
  • 383
  • 1
  • 9
  • 29

2 Answers2

0

Angular does not ship a sort pipe because they seem to think it degrades performance to an unacceptable degree. However, I have employed them and found them to be useful. You just need to write one.

import {Pipe} from '@angular/core';

@Pipe({name: 'sortBy'}) export class SortBy {
  transform<T>(
    values: T[] | undefined, 
    by: keyof T | ((x: T) => any), 
    direction?: 'ascending' | 'descending'
  ) {
   const thrust = direction || 'ascending';

   const getKey = typeof by === 'function' ? by : ((x: T) => x[by]);

   return values && values.slice().sort((x, y) => {
     const xKey = String(getKey(x));
     const yKey = String(getKey(y));
     return thrust === 'ascending'
       ? xKey.localeCompare(yKey)
       : yKey.localeCompare(xKey);
     });
  } 
} 

Now you have a generic sorting pipe that can take the property name is a string or a function that returns a property value, and optionally a direction.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • do i need to add this on service? or should a create new component from this? – Jydon Mah Nov 13 '17 at 07:24
  • @JydonMah neither, it's a pipe. declare it in the module that contains the components that will use it. – Aluan Haddad Nov 13 '17 at 07:25
  • can you make working example with your answer? i mean by plunker? – Jydon Mah Nov 13 '17 at 07:25
  • im getting error when i just copy and paste it to my component. – Jydon Mah Nov 13 '17 at 07:25
  • @JydonMah possibly later, but I know it works. I've used it many times. If you understand how pipes are used in your template then you will be able to use it too. – Aluan Haddad Nov 13 '17 at 07:26
  • What sort of error? Also, the code is showing there is to declare a pipe that you can then use it's not meant to be pasted into a component directly – Aluan Haddad Nov 13 '17 at 07:27
  • Cannot invoke an expression whose type lacks a call signature. Type keyof T | ((x: T) => any) .. that one. – Jydon Mah Nov 13 '17 at 07:29
  • @JydonMah that's odd. Are you using `--strict`? – Aluan Haddad Nov 13 '17 at 07:34
  • compiler.js:466 Uncaught Error: Unexpected value 'AppComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation. – Jydon Mah Nov 13 '17 at 07:35
  • nope i didnt use --strict on it. i updated the error shown in console. – Jydon Mah Nov 13 '17 at 07:36
  • @JydonMah I'd need to see where you are declaring it. That is a very vague error – Aluan Haddad Nov 13 '17 at 07:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158831/discussion-between-jydon-mah-and-aluan-haddad). – Jydon Mah Nov 13 '17 at 07:37
  • it works when i use keyOf instead of keyof but i still have error saying error TS1005: '=' excpected. – Jydon Mah Nov 13 '17 at 07:41
  • `keyOf` is not valid. `keyof` is a special type operator-keyword that creates a union type of all the property names of `T`. Make sure you're using a recent version of typescript at least 2.3.4. If you can't upgrade replace `keyof T` with `string`. You should be using the latest version though – Aluan Haddad Nov 13 '17 at 07:47
  • @JydonMah damn I have a syntax error on the third line of the function body. Sorry about that – Aluan Haddad Nov 13 '17 at 07:50
  • hahaha thats ok, so what is exactly the right syntax for that? – Jydon Mah Nov 13 '17 at 07:59
-1

you can refer this example of sorting numbers and words. this is inspired from http://embed.plnkr.co/DHLVc0

Example use

Basic Array of single type: *ngFor="#todo of todoService.todos | orderBy : '-'"

Multidimensional Array Sort on single column: *ngFor="#todo of todoService.todos | orderBy : ['-status']"

Multidimensional Array Sort on multiple columns: *ngFor="#todo of todoService.todos | orderBy : ['status', '-title']"

orderBy.ts

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

        @Pipe({name: 'orderBy', pure: false})
        export class OrderBy implements PipeTransform {

            static _orderByComparator(a:any, b:any):number{

              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
            }

            transform(input:any, [config = '+']): any{

                if(!Array.isArray(input)) return input;

                if(!Array.isArray(config) || (Array.isArray(config) && config.length == 1)){
                    var propertyToCheck:string = !Array.isArray(config) ? config : config[0];
                    var desc = propertyToCheck.substr(0, 1) == '-';

                    //Basic array
                    if(!propertyToCheck || propertyToCheck == '-' || propertyToCheck == '+'){
                        return !desc ? input.sort() : input.sort().reverse();
                    }
                    else {
                        var property:string = propertyToCheck.substr(0, 1) == '+' || propertyToCheck.substr(0, 1) == '-'
                            ? propertyToCheck.substr(1)
                            : propertyToCheck;

                        return input.sort(function(a:any,b:any){
                            return !desc 
                                ? OrderBy._orderByComparator(a[property], b[property]) 
                                : -OrderBy._orderByComparator(a[property], b[property]);
                        });
                    }
                }
                else {
                    //Loop over property of the array in order and sort
                    return input.sort(function(a:any,b:any){
                        for(var i:number = 0; i < config.length; i++){
                            var desc = config[i].substr(0, 1) == '-';
                            var property = config[i].substr(0, 1) == '+' || config[i].substr(0, 1) == '-'
                                ? config[i].substr(1)
                                : config[i];

                            var comparison = !desc 
                                ? OrderBy._orderByComparator(a[property], b[property]) 
                                : -OrderBy._orderByComparator(a[property], b[property]);

                            //Don't return 0 yet in case of needing to sort by next property
                            if(comparison != 0) return comparison;
                        }

                        return 0; //equal each other
                    });
                }
            }
        }
  • Not bad but a pure pipe is preferable. Also your static method should just be a free function. What I don't understand is why you would annotate a function that would have a strong and easily inferred return type with an explicit `any` – Aluan Haddad Nov 13 '17 at 06:23