Replies: 5 comments 2 replies
-
Hi Pascal, doesn't know why "Array.sort" don't work atm, but maybe you can use this view function, to get a sorted list of numbers: function (doc) {
if(doc.zone) {
doc.zone.forEach(zone_el => {
emit([doc._id, zone_el.number], null);
});
}
} You iterate through the zone array and put the numbers into the key so that you get sorting for "free". |
Beta Was this translation helpful? Give feedback.
-
Hi Pascal, testing and thinking about your problem... If you use the sort-function on the document array "zone", it would in-place sort the array. This is not allowed in a view function, because this would change the whole document (the "zone" element)! What you need to do instead is to copy the array, sort the copy and emit the result. For example you can use this map function: function (doc) {
if(doc.zone) {
let zone_sorted = [...doc.zone].sort((a, b) => a.number - b.number);
emit(doc._id, zone_sorted);
}
} The answer is a little bit hidden here:
|
Beta Was this translation helpful? Give feedback.
-
Hi, The Array is well sorted by the Big-r81 solution. function (doc) { And why emited element in the zone_sorted are not sorted ? thank you for your help. |
Beta Was this translation helpful? Give feedback.
-
Hi, but why would you do that? Then you can use the first provided approach. Maybe @janl can help with the js part? Ronny |
Beta Was this translation helpful? Give feedback.
-
Hi Pascal,
I can't tell you at the moment why this isn't working (needs further investigation), but you have other possibilities that I mentioned which are working. The question you have to ask yourself is how you want to make the data of a view available to the user. I can't answer this for you, but I might be able to help you create a map reduce function. Please read the documentation for design documents, it explains a lot. Afterwards, tell your problems and we try to create a view that shows you your data as you (hopefully) want it. Ronny |
Beta Was this translation helpful? Give feedback.
-
Hello,
I have troubles with the sort Javascript with documents.
I use CouchDb in windows 2012R2 :
{
"couchdb": "Welcome",
"version": "3.2.0",
"git_sha": "efb409bba",
"uuid": "cd3e089da5227286e25d8d0155ba59f7",
"features": [
"access-ready",
"partitioned",
"pluggable-storage-engines",
"reshard",
"scheduler"
],
"vendor": {
"name": "The Apache Software Foundation"
}
}
I found several articles on the Stack Overflow site, but I’m stuck.
My documents look like this example :
{
"_id": "DOC:10",
"rev": "1-bd10f4e4000d236b3fe4ffb5852f0204",
"sequence": 10,
"composedflow": 3262656,
"userid": "2010000758915",
"composedflowuserid": "1XXXXL.P.VALID_CTX_V5.PCTXB5-B_055_R3_100114_00001428_94----------.00091614.000028563",
"date": "2009-09-07T00:00:00",
"zone": [
{
"label": "Code modèle",
"value": "ADV013",
"number": 9
},
{
"label": "Index Générique B",
"value": "null",
"number": 41
},
{
"label": "Index Générique A",
"value": "null",
"number": 40
},
{
"label": "Numéro d'inscription au répertoire (N° INSEE ou Sécu)",
"value": "null",
"number": 39
},
{
"label": "Identifiant Applicatif National (allocataire / Affilié)",
"value": "null",
"number": 38
}
]
}
I would like to create a view by sorting the "zone" area objects by the "number" field.
Nothing could be simpler.
First view :
function (doc) {
if(doc.zone) {
var list = doc.zone;
emit( doc._id, list);
}
}
I do get an object array in the value with fauxton 👍
Second view :
function (doc) {
if(doc.zone) {
var list = doc.zone.sort(function(a,b) { return a.number-b.number });
emit( doc._id, list);
}
}
"No Documents Found" by Fauxton in less than a second. There are 12,000 Json Documents 👎
My third view :
function (doc) {
if(doc.zone) {
var temp = [
{
"label": "Code modèle",
"value": "ADV013",
"number": 9
},
{
"label": "Index Générique B",
"value": "null",
"number": 41
},
{
"label": "Index Générique A",
"value": "null",
"number": 40
},
{
"label": "Numéro d'inscription au répertoire (N° INSEE ou Sécu)",
"value": "null",
"number": 39
},
{
"label": "Identifiant Applicatif National (allocataire / Affilié)",
"value": "null",
"number": 38
}
];
var list = temp.sort(function(a,b) { return a.number-b.number });
emit( doc._id, list);
}
}
Good but static data 👍 👎
Thank you so much for your help.
Pascal
Beta Was this translation helpful? Give feedback.
All reactions