0

I have to sort an array of objects based on the user input for which I call a function on ng-change. The array needs to be sorted with respect to name in the array of objects.

Array is

[{"id":1,"name":"android","taggings_count":5,"category":null},{"id":2,"name":"ruby","taggings_count":7,"category":null},{"id":3,"name":"java","taggings_count":3,"category":null}]

Function signature is

$scope.searchTags = (tagText) ->

where tagText is the argument which I receive from ng-change

So, how can the user input i.e, tagText be mapped to name attribute in the array and compared ?

kartik
  • 666
  • 5
  • 21
  • possible duplicate of [Sort array of objects by property value in JavaScript](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-property-value-in-javascript) – Andreas Jun 10 '15 at 11:37
  • [`orderBy` filter](https://docs.angularjs.org/api/ng/filter/orderBy) – Grundy Jun 10 '15 at 11:41
  • I don't need direct comaprison with name attribute. I need to sort on string that can be mapped to name attribute in array – kartik Jun 10 '15 at 11:43
  • @kartik, can you explain what you mean? – Grundy Jun 10 '15 at 11:44
  • @Grundy : `orderBy` needs an attribute to be from an array which is not in this case. In this case it's user given input – kartik Jun 10 '15 at 11:45
  • methinks you bit confused with _orderBy_ can you provide some sample with expected output? – Grundy Jun 10 '15 at 11:47
  • @Grundy: User will send a string based on which the array needs to be sorted on basis of `name` attribute – kartik Jun 10 '15 at 11:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80170/discussion-between-grundy-and-kartik). – Grundy Jun 10 '15 at 11:47

2 Answers2

1

You can use orderBy with custom comparer, something like in next sample. Here on top would be object where name started with entered text

angular.module('app', [])
  .controller('ctrl', function($scope, $filter) {
    $scope.arr = [{
      "id": 1,
      "name": "android",
      "taggings_count": 5,
      "category": null
    }, {
      "id": 2,
      "name": "ruby",
      "taggings_count": 7,
      "category": null
    }, {
      "id": 3,
      "name": "java",
      "taggings_count": 3,
      "category": null
    }, {
      "id": 4,
      "name": "javascript",
      "taggings_count": 3,
      "category": null
    }];

    var orderBy = $filter('orderBy');
    $scope.searchedText = function(txt) {
      $scope.ordered = orderBy($scope.arr, function(el){
        if(el.name.startsWith(txt)){
          return el.name.replace(txt,'').length;
        }
        
        return 100;
        
      });
    }
    $scope.searchedText('');
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <input ng-model="text" ng-change="searchedText(text)" type="text" />
  <div ng-repeat="a in ordered">{{a}}</div>
</div>
Grundy
  • 13,356
  • 3
  • 35
  • 55
0

You can use the following code by KnightCoder.

/*
 * RandomKnight - Gists of code to get random values from numbers and arrays
 * * * * Small, fast and efficient tool
 */

if (!Math.prototype.getRandomValueBetween) {
    Math.prototype.getRandomValueBetween = function (from, to) {
        return Math.floor(Math.random() * (to - from + 1) + from);
    };
}
//USAGE -- Math.getRandomValueBetween(100,1000) //950


if (!Array.prototype.getRandom) {
    Array.prototype.getRandom = function () {
        return this[Math.getRandomValueBetween(0, this.length - 1)];
    };
}
//USAGE -- [1,34,56,76,9,67,5].getRandom();
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109