排序问题

Microsoft777 2008-04-29 09:47:20
有多个文件
这些文件有不同的类型
类型间需要排序
相同类型的排在一起
同类型也要排序
比如:
无序的情况
keke.txt
www.doc
miaomiao.txt
haha.dat
lala.doc
按照升序排序后
haha.dac
lala.doc
www.doc
keke.txt
miaomiao.txt

我的思路是这样的
做一个类似双端队列的数据结构
有一个 vector, 里面存取指向每一种类型的指针
每一种类型用 list 存储

不知道这样做如何
有什么更好的办法没有没有
谢谢
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rushman 2008-04-29
  • 打赏
  • 举报
回复
随手写的,参考一下
// 比较文件名后缀(扩展名)
int CompFileSuffix(const char* file1, const char* file2){
const* suffix1 = strrchr(file1, ',');
const* suffix2 = strrchr(file2, ',');
return strcmp(suffix1, suffix2);
}

// 比较文件名前缀(除扩展名)
int CompFilePrefix(const char* file1,const char* file2){
const char* suffix1 = strrchr(file1, ',');
const char* suffix2 = strrchr(file2, ',');
size_t len1, len2, comp_len;
int resu;

len1 = suffix1 - file1;
len2 = suffix2 - file2;
comp_len = MIN(len1, len2);

resu = strncmp(file1, file2, comp_len);

if(resu == 0)return suffix1 - suffix2;

return resu;
}

// 比较文件名
int CompFileName(const char* file1, const char* file2){
int resu;
resu = CompFileSuffix(file1, file2);

if(resu == 0)return CompFilePrefix(file1, file2);

return resu;
}
Jim_King_2000 2008-04-29
  • 打赏
  • 举报
回复
不知道这些文件名是如何获取的。如果获取这些文件名本身速度比较慢的话,可以用插入排序。或者用map(需要自己写一个比较大小的函数)。如果文件名早已存储在容器中,那么只能使用sort泛型算法(同样需要写一个自定义的比较大小函数)。
thenextday 2008-04-29
  • 打赏
  • 举报
回复
后缀用名字数,末级节点做前缀也用名字树,可以复用代码。
ryfdizuo 2008-04-29
  • 打赏
  • 举报
回复
先实现了,再效率
just do it.
laolaoliu2002 2008-04-29
  • 打赏
  • 举报
回复


file
/ \
后缀1 后缀2
/ | \ / | \
n1 n2 n3 na1...nax

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧