-2

I've been searching for this and can't quite figure out how to word (or just can't find) what I am looking for.

I have two javascript arrays of objects. One has a collection of requests full of IDs, the other contains an array that is basically a dictionary of what those IDs represent. I want to orderBy the requests array by a description_text that lives inside the dictionary array. I am trying to only house the IDs inside the request array as opposed to repeating them 1000 times in each array and am looking for the fastest way to do this.

Example:

        $scope.requests = [
                    { request_id 1, request_division_id: 1, morefeilds...}, 
                    { request_id 2, request_division_id: 3, morefeilds...}, 
                    { request_id 3, request_division_id: 1, morefeilds...}, 
                    { request_id 4, request_division_id: 2, morefeilds...}, 
                    { request_id 5, request_division_id: 4, morefeilds...}];
        $scope.divDict = [ 
            {request_division_id: 1, desc_text: 'Sahara Square'},
            {request_division_id: 2, desc_text: 'Little Rodentia'},
            {request_division_id: 3, desc_text: 'Rainforest District'},
            {request_division_id: 4, desc_text: 'Tundratown'},]

So in this case I would need the orderby to return sorted in this order:

        $scope.requests = [
                    { request_id 4, request_division_id: 2, morefeilds...}, 
                    { request_id 2, request_division_id: 3, morefeilds...}, 
                    { request_id 1, request_division_id: 1, morefeilds...}, 
                    { request_id 3, request_division_id: 1, morefeilds...}, 
                    { request_id 5, request_division_id: 4, morefeilds...}];

As that is the alphabetical order according to each id's desc_text.

Amit
  • 45,440
  • 9
  • 78
  • 110
ModestMonk
  • 123
  • 1
  • 1
  • 10
  • Possible duplicate of [Sort array of objects by string property value in JavaScript](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – VLAZ Aug 16 '16 at 19:40
  • No this is not a duplicate to that question. I am comparing to a dictionary, this question already has the properties inside the object array. I am trying to avoid that. – ModestMonk Aug 16 '16 at 19:43
  • Let me analyze this. My question is marked off-topic because I asked how do I sort an array based off another array's order. This is off topic? Thank you plalx for taking the time to explain it. I even put a Zootopia reference in hopes of not being bashed, so much for that. Stack overflow community is both kind and rude af. – ModestMonk Aug 17 '16 at 17:57

1 Answers1

0

This is not very different from sorting as if the request housed the desc_text property: rather than resolving the description from the object in the array you will resolve it from the divDict object map.

$scope.requests = $scope.requests.sort(function (req1, req2) {
    let desc1 = $scope.divDict[req1.request_id].desc_text,
        desc2 = $scope.divDict[req2.request_id].desc_text;

    return desc1 < desc2? -1 : +(desc1 > desc2);
});

EDIT:

I had not carefully read the code and expected the divDict to be an object map where the keys would be the request IDs, but I think you will still get the idea.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • Thanks. Not sure why I couldn't think of it this way. How would I go about sorting if the request_id and desc_text could potentially be dynamic or any one of the fields? – ModestMonk Aug 16 '16 at 20:20
  • I'm not sure that I understand your question. If you do not know the name of the properties that you whish to compare then the best you could do is a loop over keys and compare together similar keys or something like this. – plalx Aug 16 '16 at 20:30
  • So if passed a value, lets call it idPassedFromNgClick your code would be scope.divDict[req1.request_id][idPassedFromNgClick]? Also, why are you refering to the request_id, shouldn't it be something else as the dictionary doesn't have that property. – ModestMonk Aug 16 '16 at 20:36
  • Well sorry, I overlooked the fact that your dictionary is not one, it's an array. I expected the dictionary to be something like `{ 1: { desc_text: 'some desc'} }`. A dictionary is a key-value pair. – plalx Aug 16 '16 at 20:41
  • Sorry my terminology is off. It is an array that I am using to map the order. – ModestMonk Aug 16 '16 at 20:46
  • @ModestMonk In that case you just have to find the record matching the id in the divDict array and get the description from it. As for the other question related to idPassedFromNgClick, do you mean that the id property name would be dynamically passed? I'm not following you there. – plalx Aug 16 '16 at 20:53
  • Yeah, basically I have column headers that are sorting when you click. Let's say it looks like this: ng-click="setSort('request_division_id', divDict, 'desc_text')"> – ModestMonk Aug 16 '16 at 20:59
  • Ok well, the bracket notation lets you access properties dynamically. E.g. `yourObject[yourDynamicProperty]`. – plalx Aug 16 '16 at 21:27