+ while (*parms)
+ {
+ char parm[256];
+ char *pp;
+ const char *cpp;
+ int increasing = 0;
+ int i;
+ int offset = 0;
+ enum conf_sortkey_type type = Metadata_sortkey_string;
+ struct reclist_sortparms *new;
+
+ if (!(cpp = strchr(parms, ',')))
+ cpp = parms + strlen(parms);
+ strncpy(parm, parms, cpp - parms);
+ parm[cpp-parms] = '\0';
+
+ if ((pp = strchr(parm, ':')))
+ {
+ if (pp[1] == '1')
+ increasing = 1;
+ else if (pp[1] == '0')
+ increasing = 0;
+ else
+ {
+ yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
+ return 0;
+ }
+
+ if (pp[2])
+ {
+ if (pp[2] == 'p')
+ type = Metadata_sortkey_position;
+ else
+ yaz_log(YLOG_FATAL, "Bad sortkey modifier: %s", parm);
+ }
+ *pp = '\0';
+ }
+ if (type != Metadata_sortkey_position)
+ {
+ if (!strcmp(parm, "relevance"))
+ {
+ type = Metadata_sortkey_relevance;
+ }
+ else if (!strcmp(parm, "position"))
+ {
+ type = Metadata_sortkey_position;
+ }
+ else
+ {
+ for (i = 0; i < service->num_sortkeys; i++)
+ {
+ struct conf_sortkey *sk = &service->sortkeys[i];
+ if (!strcmp(sk->name, parm))
+ {
+ type = sk->type;
+ if (type == Metadata_sortkey_skiparticle)
+ type = Metadata_sortkey_string;
+ break;
+ }
+ }
+ if (i >= service->num_sortkeys)
+ {
+ yaz_log(YLOG_FATAL, "Sortkey not defined in service: %s",
+ parm);
+ return 0;
+ }
+ offset = i;
+ }
+ }
+ new = *rp = nmem_malloc(nmem, sizeof(struct reclist_sortparms));
+ new->next = 0;
+ new->offset = offset;
+ new->type = type;
+ new->increasing = increasing;
+ new->name = nmem_strdup(nmem, parm);
+ rp = &new->next;
+ if (*(parms = cpp))
+ parms++;
+ }
+ return res;
+}
+
+static int reclist_cmp(const void *p1, const void *p2)
+{
+ struct reclist_sortparms *sortparms =
+ (*(struct reclist_bucket **) p1)->sort_parms;
+ struct record_cluster *r1 = (*(struct reclist_bucket**) p1)->record;
+ struct record_cluster *r2 = (*(struct reclist_bucket**) p2)->record;
+ struct reclist_sortparms *s;
+ int res = 0;
+
+ for (s = sortparms; s && res == 0; s = s->next)
+ {
+ union data_types *ut1 = r1->sortkeys[s->offset];
+ union data_types *ut2 = r2->sortkeys[s->offset];
+ const char *s1, *s2;
+ switch (s->type)
+ {
+ case Metadata_sortkey_relevance:
+ res = r2->relevance_score - r1->relevance_score;
+ break;
+ case Metadata_sortkey_string:
+ s1 = ut1 ? ut1->text.sort : "";
+ s2 = ut2 ? ut2->text.sort : "";
+ res = strcmp(s2, s1);
+ if (res)
+ {
+ if (s->increasing)
+ res *= -1;
+ }
+ break;
+ case Metadata_sortkey_numeric:
+ if (ut1 && ut2)
+ {
+ if (s->increasing)
+ res = ut1->number.min - ut2->number.min;
+ else
+ res = ut2->number.max - ut1->number.max;
+ }
+ else if (ut1 && !ut2)
+ res = -1;
+ else if (!ut1 && ut2)
+ res = 1;
+ else
+ res = 0;
+ break;
+ case Metadata_sortkey_position:
+ if (r1->records && r2->records)
+ {
+ int pos1 = 0, pos2 = 0;
+ struct record *rec;
+ for (rec = r1->records; rec; rec = rec->next)
+ if (pos1 == 0 || rec->position < pos1)
+ pos1 = rec->position;
+ for (rec = r2->records; rec; rec = rec->next)
+ if (pos2 == 0 || rec->position < pos2)
+ pos2 = rec->position;
+ res = pos1 - pos2;
+ }
+ break;
+ default:
+ yaz_log(YLOG_WARN, "Bad sort type: %d", s->type);
+ res = 0;
+ break;
+ }
+ }
+ if (res == 0)
+ res = strcmp(r1->recid, r2->recid);
+ return res;