Last active
October 12, 2022 00:27
-
-
Save marguerite/d1f4c605de271225a9d67581fdf25933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dirent.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "unicode/ucol.h" | |
int main(int argc, char * argv[]) { | |
DIR * d; | |
struct dirent * dir; | |
d = opendir(argv[1]); | |
int size = 0; | |
if (d) { | |
while ((dir = readdir(d)) != NULL) { | |
char * name = dir -> d_name; | |
if (name[0] == '.') { | |
continue; | |
} | |
size++; | |
} | |
closedir(d); | |
} | |
d = opendir(argv[1]); | |
char * files[size]; | |
char * files1[size]; | |
int idx = 0; | |
if (d) { | |
while ((dir = readdir(d)) != NULL) { | |
char * name = dir -> d_name; | |
if (name[0] == '.') { | |
continue; | |
} | |
files[idx] = name; | |
files1[idx] = name; | |
idx++; | |
} | |
} | |
closedir(d); | |
for (int i = 0; i < size - 1; i++) { | |
for (int j = i + 1; j < size; j++) { | |
if ((strcoll(files[i], files[j])) > 0) { | |
char * tmp; | |
tmp = files[i]; | |
files[i] = files[j]; | |
files[j] = tmp; | |
} | |
} | |
} | |
for (int i = 0; i < size; i++) { | |
printf("%s\n", files[i]); | |
} | |
printf("-------------------\n"); | |
UErrorCode status = U_ZERO_ERROR; | |
UCollator * coll = ucol_open("zh_CN", & status); | |
if (U_SUCCESS(status)) { | |
for (int i = 0; i < size - 1; i++) { | |
for (int j = i + 1; j < size; j++) { | |
UChar source[256]; | |
UChar target[256]; | |
int32_t sourceCap = 256; | |
int32_t pSourceLen; | |
int32_t targetCap = 256; | |
int32_t pTargetLen; | |
UErrorCode err = U_ZERO_ERROR; | |
u_strFromUTF8(source, sourceCap, & pSourceLen, files1[i], strlen(files1[i]) + 1, & err); | |
u_strFromUTF8(target, targetCap, & pTargetLen, files1[j], strlen(files1[j]) + 1, & err); | |
if (ucol_strcoll(coll, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER) { | |
char * tmp1; | |
tmp1 = files1[i]; | |
files1[i] = files1[j]; | |
files1[j] = tmp1; | |
} | |
} | |
} | |
ucol_close(coll); | |
} | |
for (int i = 0; i < size; i++) { | |
printf("%s\n", files1[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不一样的,strcoll 是 unicode codepoint 排序。libicu collator 才有比如 pinyin 这样的排序