1

I search how to sort by place.city this kind of object who have id's for keys. The need is to keep id's for first keys…

{
    "123": {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "Bob",
            "age": 45
        }
    },
    "456": {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Louis",
            "age": 34
        }
    },
    "789": {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Kevin",
            "age": 27
        }
    }
}

I try some kind of function like this and the expected result is not here.

let result = _(myObject).map(function (value, key) {
    return _.defaults({ name: key }, value)
}).sortBy('city').value()
imanu
  • 13
  • 1
  • 7

5 Answers5

1

You can't sort an object.. You can, however, convert your object to an array and sort that.

var data ={
  "123" : {
    "place": {
      "city": "New York",
      "country": "USA" 
    },
    "person": {
      "name": "Bob",
      "age": 45
    }
  },
  "456" : {
    "place": {
      "city": "Chicago",
      "country": "USA" 
    },
    "person": {
      "name": "Louis",
      "age": 34
    }
  },
  "789" : {
    "place": {
      "city": "Dallas",
      "country": "USA" 
    },
    "person": {
      "name": "Kevin",
      "age": 27
    }
  }
};

var sortedByPlace = _.sortBy(Object.keys(data).map(k => ({id:k, ...data[k]})), (d)=> d.place.city)

console.log(sortedByPlace);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
1

It is not possible to sort an object, you need to make it a list.

import { map, flow } from 'lodash'
import { sortBy } from 'lodash/fp'

cities => flow(
  map(places, (place, id) => { id, ...place }),
  sortBy('city'),
)()

Your second question begs the question (mh...) if you want local sort. That would be

import { mapValues } from 'lodash'
import { sortBy } from 'lodash/fp'

data => mapValues(data, sortBy('place.city'))
Markus
  • 1,598
  • 2
  • 13
  • 32
0

If you look at this answer, the following should work

var sort = function (prop, arr) {
    prop = prop.split('.');
    var len = prop.length;

    arr.sort(function (a, b) {
        var i = 0;
        while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
    });
    return arr;
};
sort("place.city", myObject);
0

You cant sort an object. However you could create a sorted array that contains references to the objects:

  const sorted = Object.values(myObject).sort((a, b) => a.place.city.localeCompare(b.place.city));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • True, it's a smart solution. Doesn't work with my updated object here, I missed a point in first post (id are used in sub objects too) https://stackoverflow.com/a/49877763/4075544 – imanu Apr 25 '18 at 11:30
0

I realize the object I have to treat (get from an obscur API) is a little bit more complicated than my first example :/

So the responses you nicely give are not working anymore…

Do you see the subtlety on this new object ?

The point stay to sort by place.city

{
    "123": {
      0: {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "Bob",
            "age": 45
        }
      },
      1: {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "James",
            "age": 32
        }
      }
    },
    "456": {
      0: {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Louis",
            "age": 34
        }
      },
      1: {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Christine",
            "age": 65
        }
      }
    },
    "789": {
      0: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Kevin",
            "age": 27
        }
      },
      1: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Robert",
            "age": 55
        }
      },
      2: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Danny",
            "age": 62
        }
      }
    }
}
imanu
  • 13
  • 1
  • 7