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()

5 Answers 5

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/[email protected]/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

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'))

Comments

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));

1 Comment

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) stackoverflow.com/a/49877763/4075544
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);

Comments

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
        }
      }
    }
}

1 Comment

Please edit your question and don't write it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.