Page 33 - Dart Language
P. 33

Knowing this, we can sort a list of integers using an anonymous function.


         List<int> numbers = [4,1,3,5,7];

         numbers.sort((int a, int b) {
            if(a == b) {
               return 0;
            } else if (a < b) {
               return -1;
            } else {
               return 1;
            }
         });


        Anonymous function may also be bound to identifiers like so:


         Function intSorter = (int a, int b) {
            if(a == b) {
               return 0;
            } else if (a < b) {
               return -1;
            } else {
               return 1;
            }
         }


        and used as an ordinary variable.


         numbers.sort(intSorter);


        Read Functions online: https://riptutorial.com/dart/topic/2965/functions











































        https://riptutorial.com/                                                                               28
   28   29   30   31   32   33   34   35   36   37   38