Function

uni_sortkeycmp

Compare sort keys.

Since v1.0
unistat uni_sortkeycmp(
const uint16_t *sk1, size_t sk1_len, const uint16_t *sk2, size_t sk2_len, int32_t *result)

Parameters 🔗

sk1 in

The first sort key.

sk1_len in

Length of sort key sk1.

sk2 in

The second sort key.

sk2_len in

Length of sort key sk2.

result out

The integer -1, 0, or 1 depending on if s1 < s2, s1 = s2, or s1 > s2.

Return Value 🔗

UNI_OK

If the collation algorithm executed successfully.

UNI_BAD_OPERATION

If sk1, sk2, or result are NULL.

Discussion 🔗

Compare sort keys sk1 and sk2 and write either -1, 0, or 1 to result depending on if sk1 < sk2, sk1 = sk2, or sk1 > sk2. The sort keys must be generated with uni_sortkeymk.

Examples 🔗

This example constructs two sort keys and compares them. Sort keys must be generated with the same settings for their order to make sense. In this example, the sort keys are constructed with UNI_SHIFTED weighting and UNI_PRIMARY strength. In a real application you would cache the sort keys for future comparisons. For one-off comparisons, use uni_collate.

#include <unicorn.h>
#include <stdio.h>

int main(void)
{
    const char *s1 = "hello", *s2 = "Hi";
    uint16_t sk1[16] = {0}, sk2[16] = {0};
    size_t sk1_len = 16, sk2_len = 16;

    if (uni_sortkeymk(s1, -1, UNI_UTF8, UNI_SHIFTED, UNI_PRIMARY, sk1, &sk1_len) != UNI_OK ||
        uni_sortkeymk(s2, -1, UNI_UTF8, UNI_SHIFTED, UNI_PRIMARY, sk2, &sk2_len) != UNI_OK)
    {
        puts("failed to construct sort keys");
        return 1;
    }

    int result;
    if (uni_sortkeycmp(sk1, sk1_len, sk2, sk2_len, &result) != UNI_OK)
    {
        puts("failed to compare sort keys");
        return 1;
    }

    printf("%d", result);
    return 0;
}