5eee234b477ed74599b35af7cbad1de195fae16b kent Sat Jan 21 14:18:32 2017 -0800 Adding cmpStringOrder routine to force order to conform to a predefined explicit nonalphabetical order. diff --git src/lib/common.c src/lib/common.c index 42bf462..647513e 100644 --- src/lib/common.c +++ src/lib/common.c @@ -2451,30 +2451,49 @@ while (nextStringInList(&p) != NULL) cnt++; return cnt; } int stringArrayIx(char *string, char *array[], int arraySize) /* Return index of string in array or -1 if not there. */ { int i; for (i=0; i<arraySize; ++i) if (!differentWord(array[i], string)) return i; return -1; } +int cmpStringOrder(char *a, char *b, char **orderFields, int orderCount) +/* Compare two strings to sort in same order as orderedFields. If strings are + * not in order, will sort them to be after all ordered fields, alphabetically */ +{ +int aIx = stringArrayIx(a, orderFields, orderCount); +int bIx = stringArrayIx(b, orderFields, orderCount); +if (aIx < 0) // A not in list? + { + if (bIx < 0) // Neither in list, be alphabetical + return(strcmp(a, b)); + else // Only b in list, move a towards end + return 1; + } +else if (bIx < 0) // Only a in list, move b towards end + return -1; +else + return aIx - bIx; // Both in ordered list, just subtract indexes to sort +} + int ptArrayIx(void *pt, void *array, int arraySize) /* Return index of pt in array or -1 if not there. */ { int i; void **a = array; for (i=0; i<arraySize; ++i) { if (pt == a[i]) return i; } return -1; }