I´m striving to get some aggregated data from Mongo DB. I have the following collections:
I´m striving to get some aggregated data from Mongo DB. I have the following collections:
I have 2 collections:
product
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feec",
"5eb92a7909823240081cd763"
],
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X10",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
},
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feed",
"5eb92a7909823240081cd763"
],
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X11",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}
2. attribute
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : [
{
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd764"),
"name" : "RED",
"updatedAt" : ISODate("2020-06-01T13:47:39.236Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
}
],
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : [
{
"is_active" : true,
"_id" : ObjectId("5eb92c4338d0b83a3088feeb"),
"name" : "M",
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"createdAt" : ISODate("2020-05-11T10:43:15.355Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feec"),
"name" : "S",
"updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feed"),
"name" : "XXL",
"updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
}
],
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
}
I want to this type of result
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feec",
"5eb92a7909823240081cd763"
],
"attributes" : [
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feec"),
"name" : "S",
"updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
}
]
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X10",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
},
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feed",
"5eb92a7909823240081cd763"
],
"attributes" : [
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feed"),
"name" : "XXL",
"updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
}
]
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X11",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}
// ------------ Here variants array values are attribute's slug ID -----------------
Please Help Me
Please let me know how?
That's not very effective data design: one of your collections uses ids of parts of documents in other collection. First of all you need to consider having slug._id: 1 index for attributes. Then to fetch products with included attributes which have filtered slugs use something like
db.product.find(...).map(function(prod){
prod.attributes = [];
for(sid in prod.variants){
var attr = db.attributes.find({'slug._id': sid});
attr.slug = attr.slug.find(function(slug){return slug._id == sid});
prod.atributes.push(attr);
}
return prod;
})
Im trying to use underscore.js to sort my JSON object, however it doesn't seem to sort it alphabeticly when I sort by companyName?
As you can see with this code, the third object in data.adverts has doesnt start with the letter D ?
Code looks like:
var sortedDesc = _.sortBy(data.adverts, 'companyName');
$.each(sortedDesc, function () {
Doing my stuff here.....
});
The object looks like:
data({
"title" : "Krak firma API",
"query" : "http://api.eniro.com/cs/search/basic?geo_area=danmark&country=dk&search_word=DIS&version=1.1.3&callback=jQuery22003420392591506243_1456226871368",
"totalHits" : 44,
"totalCount" : 44,
"startIndex" : 1,
"itemsPerPage" : 25,
"adverts" : [{
"eniroId" : "66139761",
"companyInfo" : {
"companyName" : "Dansk Industri Service A/S",
"orgNumber" : "20250844",
"cvrPNumber" : "1004202979",
"companyText" : null
},
"address" : {
"streetName" : "Generatorvej 17",
"postCode" : "2860",
"postArea" : "Søborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.465702,
"latitude" : 55.729974
}, {
"use" : "route",
"longitude" : 12.465662,
"latitude" : 55.730202
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "70 20 70 15",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66139761/dis",
"homepage" : "http://api.eniro.com/proxy/homepage/uANwPf5aVK2cGM31dAhxnP94Fw6wLDJmjGFCQRGYZgO51Qs6_d_2i9h4MLaWRmsw_87mP_j3IjX1e2t2Hx6_yQ==",
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dansk-industri-service-as:66139761?search_word=DIS"
}, {
"eniroId" : "66295511",
"companyInfo" : {
"companyName" : "Diss",
"orgNumber" : "30924606",
"cvrPNumber" : "1013726465",
"companyText" : null
},
"address" : {
"coName" : "Rikke Søndermølle",
"streetName" : "Hovangsvej 71",
"postCode" : "9500",
"postArea" : "Hobro",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.786483,
"latitude" : 56.649997
}, {
"use" : "route",
"longitude" : 9.786596,
"latitude" : 56.650012
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "58 52 62 61",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66295511/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/diss:66295511?search_word=DIS"
}, {
"eniroId" : "66741314",
"companyInfo" : {
"companyName" : "Moster Di's Grill og Kaffevogn",
"orgNumber" : "21651605",
"cvrPNumber" : "1017404705",
"companyText" : null
},
"address" : {
"streetName" : "Hedelandsvej 14",
"postCode" : "7400",
"postArea" : "Herning",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 8.935914,
"latitude" : 56.127568
}, {
"use" : "route",
"longitude" : 8.93635,
"latitude" : 56.127463
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "97 42 04 20",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66741314/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/moster-dis-grill-og-kaffevogn:66741314?search_word=DIS"
}, {
"eniroId" : "122768415",
"companyInfo" : {
"companyName" : "Dis Byg ApS",
"orgNumber" : "37167304",
"cvrPNumber" : "1020824162",
"companyText" : null
},
"address" : {
"streetName" : "Ved Banen 8",
"postCode" : "7100",
"postArea" : "Vejle",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.553109,
"latitude" : 55.698735
}, {
"use" : "route",
"longitude" : 9.553159,
"latitude" : 55.698833
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/122768415/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-byg-aps:122768415?search_word=DIS"
}, {
"eniroId" : "121468337",
"companyInfo" : {
"companyName" : "Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Studiestræde 21 A",
"postCode" : "1455",
"postArea" : "København K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5697907,
"latitude" : 55.6784752
}, {
"use" : "route",
"longitude" : 12.5697907,
"latitude" : 55.6784752
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 12 00 00",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/121468337/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis:121468337?search_word=DIS"
}, {
"eniroId" : "108935200",
"companyInfo" : {
"companyName" : "Seapay Dis ApS",
"orgNumber" : "36054506",
"cvrPNumber" : "1019585685",
"companyText" : null
},
"address" : {
"streetName" : "Ivar Bentsens Vej 12",
"postCode" : "4300",
"postArea" : "Holbæk",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.758894,
"latitude" : 55.717305
}, {
"use" : "route",
"longitude" : 11.758669,
"latitude" : 55.717311
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/108935200/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/seapay-dis-aps:108935200?search_word=DIS"
}, {
"eniroId" : "66303436",
"companyInfo" : {
"companyName" : "Dis-Faxe",
"orgNumber" : "33686587",
"cvrPNumber" : "1016954493",
"companyText" : null
},
"address" : {
"coName" : "Gitte Willumsen Berg",
"streetName" : "Terslev Bygade 7",
"postCode" : "4690",
"postArea" : "Haslev",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.969888,
"latitude" : 55.374341
}, {
"use" : "route",
"longitude" : 11.970108,
"latitude" : 55.374433
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "30 29 98 77",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66303436/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-faxe:66303436?search_word=DIS"
}, {
"eniroId" : "68809471",
"companyInfo" : {
"companyName" : "Dis Bygge Og Ejendomsadministration",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Tuborg Boulevard 12, 1. sal.",
"postCode" : "2900",
"postArea" : "Hellerup",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5807377,
"latitude" : 55.7271613
}, {
"use" : "route",
"longitude" : 12.5807377,
"latitude" : 55.7271613
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "24 45 71 74",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/68809471/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-bygge-og-ejendomsadministration:68809471?search_word=DIS"
}, {
"eniroId" : "93544746",
"companyInfo" : {
"companyName" : "Dis Center ApS",
"orgNumber" : "35473009",
"cvrPNumber" : "1018749374",
"companyText" : null
},
"address" : {
"streetName" : "Hammershøj 38",
"postCode" : "9460",
"postArea" : "Brovst",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.596158,
"latitude" : 57.05976
}, {
"use" : "route",
"longitude" : 9.596163,
"latitude" : 57.059895
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/93544746/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-center-aps:93544746?search_word=DIS"
}, {
"eniroId" : "66949810",
"companyInfo" : {
"companyName" : "Dis-Danmark",
"orgNumber" : "16917338",
"cvrPNumber" : "1005846555",
"companyText" : null
},
"address" : {
"coName" : "Jan Amnitzbøl Krusell",
"streetName" : "Frøkærparken 116",
"postCode" : "8320",
"postArea" : "Mårslet",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 10.152582,
"latitude" : 56.07644
}, {
"use" : "route",
"longitude" : 10.152397,
"latitude" : 56.076574
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "24 63 62 69",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66949810/dis",
"homepage" : "http://api.eniro.com/proxy/homepage/uANwPf5aVK26f2J7KyRZqimid0oj_3qIjQ0XbJhXGfpb4h41yM5odSv_D17GURWQlMNOi53PltI=",
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-danmark:66949810?search_word=DIS"
}, {
"eniroId" : "123661747",
"companyInfo" : {
"companyName" : "Dis Group Holding A/S",
"orgNumber" : "37226963",
"cvrPNumber" : "1020895019",
"companyText" : null
},
"address" : {
"streetName" : "Ørstedsvej 10",
"postCode" : "8660",
"postArea" : "Skanderborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.980758,
"latitude" : 56.069991
}, {
"use" : "route",
"longitude" : 9.9793,
"latitude" : 56.070543
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/123661747/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-group-holding-as:123661747?search_word=DIS"
}, {
"eniroId" : "109604576",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Holmbladsgade 70 B",
"postCode" : "2300",
"postArea" : "København S",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.6120585,
"latitude" : 55.6648985
}, {
"use" : "route",
"longitude" : 12.6120585,
"latitude" : 55.6648985
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "32 84 11 17",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/109604576/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:109604576?search_word=DIS"
}, {
"eniroId" : "66902798",
"companyInfo" : {
"companyName" : "Dis Holding ApS",
"orgNumber" : "29180407",
"cvrPNumber" : "1011836085",
"companyText" : null
},
"address" : {
"coName" : "Rikke Søndermølle",
"streetName" : "Hovangsvej 71",
"postCode" : "9500",
"postArea" : "Hobro",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.786483,
"latitude" : 56.649997
}, {
"use" : "route",
"longitude" : 9.786596,
"latitude" : 56.650012
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "22 99 66 96",
"label" : null
}, {
"type" : "custom",
"phoneNumber" : "40 88 83 48",
"label" : "Mobil"
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66902798/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-holding-aps:66902798?search_word=DIS"
}, {
"eniroId" : "68536140",
"companyInfo" : {
"companyName" : "Danish Ins.for Study Abroad Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Ctr By ",
"postCode" : "1199",
"postArea" : "Kh K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : null,
"latitude" : null
}, {
"use" : "route",
"longitude" : null,
"latitude" : null
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 11 51 91",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/68536140/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/danish-insfor-study-abroad-dis:68536140?search_word=DIS"
}, {
"eniroId" : "112589612",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Peder Skrams Gade 23, 1. sal.",
"postCode" : "1054",
"postArea" : "København K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5895666,
"latitude" : 55.6773222
}, {
"use" : "route",
"longitude" : 12.5895666,
"latitude" : 55.6773222
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 15 05 30",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/112589612/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:112589612?search_word=DIS"
}, {
"eniroId" : "108478586",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Ravnsborggade 11",
"postCode" : "2200",
"postArea" : "København N",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5618939,
"latitude" : 55.688666
}, {
"use" : "route",
"longitude" : 12.5618939,
"latitude" : 55.688666
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "35 36 65 05",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/108478586/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:108478586?search_word=DIS"
}, {
"eniroId" : "67198842",
"companyInfo" : {
"companyName" : "Fabriksarbejder Klubben Abb Dis- Tribution",
"orgNumber" : "15624299",
"cvrPNumber" : "1005745320",
"companyText" : null
},
"address" : {
"streetName" : "Ligustervænget 9",
"postCode" : "7000",
"postArea" : "Fredericia",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.749819,
"latitude" : 55.610781
}, {
"use" : "route",
"longitude" : 9.749936,
"latitude" : 55.610541
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67198842/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fabriksarbejder-klubben-abb-dis-tribution:67198842?search_word=DIS"
}, {
"eniroId" : "104892983",
"companyInfo" : {
"companyName" : "Dis-Skive",
"orgNumber" : "35575154",
"cvrPNumber" : "1019219093",
"companyText" : null
},
"address" : {
"coName" : "Lone Hjorth Nielsen",
"streetName" : "Brombærvej 10",
"postCode" : "7800",
"postArea" : "Skive",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.027915,
"latitude" : 56.586359
}, {
"use" : "route",
"longitude" : 9.027838,
"latitude" : 56.586196
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/104892983/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-skive:104892983?search_word=DIS"
}, {
"eniroId" : "66957596",
"companyInfo" : {
"companyName" : "Dis Udlejning ApS",
"orgNumber" : "32143237",
"cvrPNumber" : "1015238832",
"companyText" : null
},
"address" : {
"streetName" : "Generatorvej 17",
"postCode" : "2860",
"postArea" : "Søborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.465702,
"latitude" : 55.729974
}, {
"use" : "route",
"longitude" : 12.465662,
"latitude" : 55.730202
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "70 20 70 15",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66957596/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-udlejning-aps:66957596?search_word=DIS"
}, {
"eniroId" : "67034343",
"companyInfo" : {
"companyName" : "DI's Risk Management Forening",
"orgNumber" : "33336047",
"cvrPNumber" : "1016630620",
"companyText" : null
},
"address" : {
"coName" : "DI, 1787 v",
"streetName" : "H.C. Andersens Boulevard 18",
"postCode" : "1553",
"postArea" : "København V",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.568025,
"latitude" : 55.675344
}, {
"use" : "route",
"longitude" : 12.568243,
"latitude" : 55.675492
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 77 33 77",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67034343/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-risk-management-forening:67034343?search_word=DIS"
}, {
"eniroId" : "66269242",
"companyInfo" : {
"companyName" : "DIs Personaleforening, DIP",
"orgNumber" : "26832039",
"cvrPNumber" : "1009390797",
"companyText" : null
},
"address" : {
"coName" : "DI, 1787 v",
"streetName" : "H.C. Andersens Boulevard 18",
"postCode" : "1553",
"postArea" : "København V",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.568025,
"latitude" : 55.675344
}, {
"use" : "route",
"longitude" : 12.568243,
"latitude" : 55.675492
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "40 19 80 90",
"label" : null
}, {
"type" : "custom",
"phoneNumber" : "30 59 05 02",
"label" : "Mobil"
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66269242/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-personaleforening-dip:66269242?search_word=DIS"
}, {
"eniroId" : "66180758",
"companyInfo" : {
"companyName" : "DIS-Næstved",
"orgNumber" : "34762996",
"cvrPNumber" : "1018256270",
"companyText" : null
},
"address" : {
"coName" : "Per Hartvig-Olsen",
"streetName" : "Tornemarksvej 38",
"postCode" : "4700",
"postArea" : "Næstved",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.561402,
"latitude" : 55.239048
}, {
"use" : "route",
"longitude" : 11.560598,
"latitude" : 55.238628
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66180758/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-næstved:66180758?search_word=DIS"
}, {
"eniroId" : "102638245",
"companyInfo" : {
"companyName" : "Dis-Sydfyn",
"orgNumber" : "35548130",
"cvrPNumber" : "1019012014",
"companyText" : null
},
"address" : {
"coName" : "Nani Krejsing",
"streetName" : "Kogtvedvej 76",
"postCode" : "5700",
"postArea" : "Svendborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 10.583639,
"latitude" : 55.048368
}, {
"use" : "route",
"longitude" : 10.583852,
"latitude" : 55.048192
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "65 31 60 31",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/102638245/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-sydfyn:102638245?search_word=DIS"
}, {
"eniroId" : "103192650",
"companyInfo" : {
"companyName" : "Dis-Kongeaaen",
"orgNumber" : "35585036",
"cvrPNumber" : "1019047985",
"companyText" : null
},
"address" : {
"coName" : "Jens christian Andersen",
"streetName" : "Toftager 16",
"postCode" : "6660",
"postArea" : "Lintrup",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 8.971892,
"latitude" : 55.409949
}, {
"use" : "route",
"longitude" : 8.971726,
"latitude" : 55.40997
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "74 85 54 23",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/103192650/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-kongeaaen:103192650?search_word=DIS"
}, {
"eniroId" : "67016411",
"companyInfo" : {
"companyName" : "DIS København Nord",
"orgNumber" : "31661811",
"cvrPNumber" : "1014736502",
"companyText" : null
},
"address" : {
"coName" : "Ann Christensen",
"streetName" : "Ellestien 1",
"postCode" : "2880",
"postArea" : "Bagsværd",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.45442,
"latitude" : 55.768364
}, {
"use" : "route",
"longitude" : 12.454607,
"latitude" : 55.768437
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "22 58 86 46",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67016411/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-københavn-nord:67016411?search_word=DIS"
}
]
});
The companyName is a property inside companyInfo so you should try something like this:
var sortedDesc = _.sortBy(data.adverts, function(a){ return a.companyInfo.companyName; });
Unfortunately there is no generic "compare" function in JavaScript to return a suitable value for sort(). I'd write a compareStrings function that uses comparison operators and then use it in the sort function.
function compareStrings(a, b) {
// Assuming you want case-insensitive comparison
a = a.toLowerCase();
b = b.toLowerCase();
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
places.sort(function(a, b) {
return compareStrings(a.city, b.city);
})
more detail visit here. Sorting JSON (by specific element) alphabetically