0

I want to sort the data according to following conditions:

  1. Sort by type with precedence as [state,city,district].

    -Data with type state should be at first place, then data with city and so on.

  2. Duplicate data should be sort by score.

    -eg : if 3 rows are with type, then sort it by score.

My data object is:

$scope.data = [{
 id: 1,
 name: 'ABC',
 type: 'city',
 score: 0.1
}, {
 id: 2,
 name: 'PQR',
 type: 'city',
 score: 0.7
}, {
 id: 3,
 name: 'ABC',
 type: 'state',
 score: 0.3
});

And template is:

<div ng-repeat="d in data">
 <span>{{d.name}}</span>
 <span>{{d.type}}</span>
 <span>{{d.score}}</span>
</div>

Result should be like this:

ABC state 0.3
PQR city 0.7
ABC city 0.1

Here is the link of plunker.

I want to sort it by multiple fields as well as with a specific precedence.

Any help will be appreciated. Thanks in advance.

Amir
  • 1,328
  • 2
  • 13
  • 27
Kiran Pawar
  • 322
  • 1
  • 2
  • 16
  • 3
    [so] is *not* a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour]. – Igor Jun 06 '17 at 15:28
  • 1
    Possible duplicate of [Javascript, how do you sort an array on multiple columns?](https://stackoverflow.com/questions/2784230/javascript-how-do-you-sort-an-array-on-multiple-columns) – Igor Jun 06 '17 at 15:31
  • I have tried sorting it by using loops. But It doesn't seems a better way to do so. I want an efficient way to do it. – Kiran Pawar Jun 06 '17 at 15:32
  • I have to sort it by multiple fields with specific precedence. It should be sort by `type` in this manner : `state, city, district`. – Kiran Pawar Jun 06 '17 at 15:41
  • Kiran it looks like you have a very clear understanding of the problem. Where exactly are you encountering difficulties solving it? – Freeman Lambda Jun 06 '17 at 15:49
  • @FreemanLambda I was using loops for this but I need an efficient way to do so. As I am implementing it on search, loops would slow down the searching. – Kiran Pawar Jun 06 '17 at 16:39

2 Answers2

1

You with find all you need in lodash library.

Here is the documentation : https://lodash.com/docs/4.17.4

A start point for you will be the orderBy method : https://lodash.com/docs/4.17.4#orderBy

To resolve your issue :

$scope.data = lodash.orderBy($scope.data, ['type','score'], ['desc', 'desc']);

You can use ngLodash for angularjs (simple install with bower / npm) : https://github.com/rockabox/ng-lodash

Enjoy!

Bibzer
  • 264
  • 2
  • 4
  • 1
    I don't understand your point. The question is about sorting data in angular. Or did i miss something about VanillaJS (I've heard of it, never used it) ? :) – Bibzer Jun 06 '17 at 16:32
1

//here is my code. I think this code solve your problem

  var tempFlag = {'state':1, 'city':2, 'district':3}
  $scope.data=[
    {
     id: 1,
     name: 'ABC',
     type: 'city',
     score: 0.1
    }, {
     id: 2,
     name: 'PQR',
     type: 'city',
     score: 0.7
    }, {
     id: 3,
     name: 'ABC',
     type: 'state',
     score: 0.3
    }
    ].sort(function(a, b) {
        if(a.type == b.type)
           return a.score < b.score;
       return tempFlag[a.type] > tempFlag[b.type];
    });
Tushar Ghosh
  • 942
  • 1
  • 12
  • 18
  • I don't think this fish will swim... Code blocks on their own are not usually useful answers, and are more likely to attract downvotes. Please explain what the solution you're showing does, and why/how that code answers the question. – Heretic Monkey Jun 06 '17 at 16:31