cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <body ng-app="myApp">
  <div ng-controller="FruitCtrl as fruitVm">
    <h1>Fruits sorted by name</h1>
    <div ng-repeat="fruit in fruitVm.fruitsSortedByName">
      {{fruit.name}} | color: {{fruit.color}}
    </div>
    
    <h1>Fruits sorted by color name</h1>
    <div ng-repeat="fruit in fruitVm.fruitsSortedByColor()">
      {{fruit.name}} | color: {{fruit.color}}
    </div>
  </div>
</body>


            
          
!
            
              var myApp = angular.module('myApp', []);

myApp.controller('FruitCtrl', function() {
  this.fruitsSortedByName = fruits.sort(sortByName);
  
  //this will also modify the reference of "fruitsSortedByName"
  //which we dont want to do
  //this.fruitsSortedByColor = fruits.sort(sortByColor); 
  
  //we want a property to hold the sorted fruits without tampering with the other property
  this.fruitsSortedByColor = function() {
    //copy the array to a new variable before sorting so we don't sort the other array by reference
    _fruits = [];
    angular.copy(fruits, _fruits);
    _fruits.sort(sortByColor);
    return _fruits;
  };
  
  function sortByName(f1, f2) {
    return (f1.name < f2.name) ? -1 : (f1.name > f2.name) ? 1 : 0;
  };
  function sortByColor(f1, f2) {
    return (f1.color < f2.color) ? -1 : (f1.color > f2.color) ? 1 : 0;
  };
});

var fruits = [
  {
    "name":"apple",
    "color":"red",
  },
  {
    "name":"grape",
    "color":"purple",
  },
  {
    "name":"banana",
    "color":"yellow",
  },
  {
    "name":"avocado",
    "color":"green",
  }
];


            
          
!
999px
Loading ..................

Console