sort排序可以有中文吗(sort方法默认什么排序规则)

http://www.itjxue.com  2023-01-28 04:26  来源:未知  点击次数: 

C语言中中文排序的问题请教!

#includestdio.h

#includestdlib.h

#includestring.h

#includectype.h

int?number;

char?key[100];

const?char?*?ErrorInputInfo?=?"输入错误,请重新输入:";

int?caidan(int?a,?int?b,?const?char?*?str?=?ErrorInputInfo)

{

????int?n,?m,?i,?len;

????while(1)

????{????

????????m?=?1;

len?=?strlen(key);

????????for(i?=?0;?i??len;?i++)

????????{

if(isdigit(key[i])?==?0)

{

m?=?0;

break;

}

????????}

????????if(m?==?0)

????????{

????????????printf("输入错误,请输入数字,请重新输入:\n");????

????????????scanf("%s",?key);

????????}

????????else

????????{

????????????n?=?atoi(key);

????????????if(n??b?||?n??a)

{

printf("%s\n",?str);

scanf("%s",?key);

}

????????????else?break;

????????}

????}

????return?n;

}

struct?worker??/*定义员工结构体*/

{

????int???nub;

????char??name[20];

????char??sex[8];

????float?gz;

????char??beizhu[10];

}?yg[100],?ls;

void?add()??//?添加员工

{

????FILE?*fp;

????printf("请输入员工信息:姓名,性别,工资,备注,工号将自动生成\n");

????scanf("%s?%s?%f?%s",?yg[number].name,?yg[number].sex,?yg[number].gz,?yg[number].beizhu);

????yg[number].nub?=?number?+?1;

????fp?=?fopen("员工数据库.txt","a+");

if(fp)

{

fprintf(fp,?"%d\t%s\t%s\t%f\t%s\n",?yg[number].nub,?yg[number].name,?yg[number].sex,?yg[number].gz,?yg[number].beizhu);

fclose(fp);

++number;

}

else

{

printf("打开文件错误,添加记录失败!\n");

}

}

int?zairu()????//?载入历史数据

{

int?i;

FILE?*fp?=?fopen("员工数据库.txt",?"r");

if(fp)

{

for(i?=?0;?!feof(fp);?i++)

fscanf(fp,?"%d?%s?%s?%f?%s",?yg[i].nub,?yg[i].name,?yg[i].sex,?yg[i].gz,?yg[i].beizhu);

fclose(fp);

--i;

printf("成功读取%d条记录!\n",?i);

return?i;

}

else

{

printf("打开文件错误,读取失败!\n");

return?0;

}

}

void?chakan(int?n?=?500)??//查看员工列表

{???

????int?i;

if(0??n?||?n??number)

{

for(i?=?0;?i??number;?i++)

{

if(i?==?0)?printf("工号\t姓名\t性别\t工资\t\t备注\n");

printf("%d\t%s\t%s\t%f\t%s\n",?yg[i].nub,?yg[i].name,?yg[i].sex,?yg[i].gz,?yg[i].beizhu);

}

}

else?printf("%d\t%s\t%s\t%f\t%s\n",?yg[n].nub,?yg[n].name,?yg[n].sex,?yg[n].gz,?yg[n].beizhu);

}

void?baocun()

{

int?i;

????FILE?*?fp?=?fopen("员工数据库.txt",?"w");

if(fp)

{

for(i?=?0;?i??number;?i++)

fprintf(fp,?"%d?%s?%s?%f?%s\n",?yg[i].nub,?yg[i].name,?yg[i].sex,?yg[i].gz,?yg[i].beizhu);

fclose(fp);

}

else?printf("打开文件错误!保存文件失败!\n");

}

void?xiugai()???//修改员工信息

{

????int?n,?m;

????printf("请输入要修改的员工工号?:?");

????scanf("%s",?key);

n?=?caidan(1,?number,?"员工工号不存在,请重新输入:")?-?1;

????chakan(n);

????printf("请输入要修改的项目:1姓名?2性别?3工资?4备注?:?");

????scanf("%s",?key);

m?=?caidan(1,?4);

????switch(m)

????{

????case?1:

????????printf("输入新的姓名:");

????????scanf("%s",?yg[n].name);

break;

????case?2:

????????printf("输入新的性别:");

????????scanf("%s",?yg[n].sex);

break;

????case?3:

????????printf("输入新的工资:");

????????scanf("%f",?yg[n].gz);

break;

????case?4:

????????printf("输入新的备注:");

????????scanf("%s",?yg[n].beizhu);

break;

????}

baocun();

}

void?chazhao()??//按关键字查找员工

{

????int?n,?m,?i,?t=0;

????float?min,max;

????printf("请选择查找类型:1工号?2姓名?3性别?4工资?5备注\n");

????scanf("%s",key);

????n?=?caidan(1,5);

????switch(n)

????{

????case?1:

????????printf("请输入要查找的工号:\n");

????????scanf("%s",key);

m?=?caidan(0,?100,?"员工号应小于100,请重新输入:");

????????for(i?=?0;?i??number;?i++)

????????{

????????????if(m?==?yg[i].nub)

{

chakan(i);

t++;

}

????????}

????????if(t?==?0)?printf("该工号不存在\n");

????????else?printf("共找到%d位符合条件的员工\n",?t);

????????break;

????case?2:

????????printf("请输入要查找的姓名:\n");

????????scanf("%s",?key);

????????for(i?=?0;?i??number;?i++)

????????{

????????????if(!strcmp(key,?yg[i].name))

{

chakan(i);

t++;

}

????????}

????????if(t?==?0)?printf("该姓名不存在\n");

????????else?printf("共找到%d位符合条件的员工\n",?t);

????????break;

????case?3:

????????printf("请输入要查找的性别:\n");

????????scanf("%s",?key);

????????for(i?=?0;?i??number;?i++)

????????{

????????????if(!strcmp(key,yg[i].sex))

{

chakan(i);

t++;

}

????????}

????????if(t?==?0)?printf("该性别不存在\n");

????????else?printf("共找到%d位符合条件的员工\n",?t);

????????break;

????case?4:

????????printf("请输入要查找的工资区间min~max:\n");

????????scanf("%f?%f",?min,?max);

????????for(i?=?0;?i??number;?i++)

????????{

????????????if(yg[i].gz?=?min??yg[i].gz?=?max)

{

chakan(i);

t++;

}

????????}

????????if(t?==?0)?printf("该工资区间没有员工存在\n");

????????else?printf("共找到%d位符合条件的员工\n",?t);

????????break;

????case?5:

????????printf("请输入要查找的备注信息:\n");

????????scanf("%s",?key);

????????for(i?=?0;?i??number;?i++)

????????{

????????????if(!strcmp(key,yg[i].beizhu))

{

chakan(i);

t++;

}

????????}

????????if(t?==?0)?printf("该备注信息不存在\n");

????????else?printf("共找到%d位符合条件的员工\n",?t);

????????break;

????}

}

#define?SORT(X)?do{for(i=0;?inumber;i++){pos=i;for(j=i+1;jnumber;j++)if(##X##)pos?=?j;if(pos!=i)ls=yg[pos],yg[pos]=yg[i],yg[i]=ls;}}while(0)

void?paixu()

{

????int?n,?m,?i,?j,?pos;

????printf("请选择排序字段:1工号?2姓名?3性别?4工资?5备注?:?");

????scanf("%s",key);

????n?=?caidan(1,5);

????switch(n)

????{

????case?1:

????????printf("请选择:1正序?2倒序?:?");

????????scanf("%s",?key);

m?=?caidan(1,?2);

????????if(m?==?1)?SORT(yg[pos].nub??yg[j].nub);

????????else?SORT(yg[pos].nub??yg[j].nub);

????????break;

????case?2:

printf("请选择:1正序?2倒序?:?");

????????scanf("%s",?key);

????????m?=?caidan(1,?2);

????????if(m?==?1)?SORT(strcmp(yg[pos].name,?yg[j].name)?==?1);

????????else?SORT(strcmp(yg[pos].name,?yg[j].name)?==?-1);

????????break;

????case?3:

break;

????case?4:

????????printf("请选择:1正序?2倒序?:?");

????????scanf("%s",?key);

????????m?=?caidan(1,?2);

????????if(m?==?1)?SORT(yg[pos].gz??yg[j].gz);

????????else?SORT(yg[pos].gz??yg[j].gz);

????????break;

????case?5:

????????break;

????}

chakan();

}

void?main()

{???

????char?key1[100];

int?m?=?0;

????number?=?zairu();

????while(m?!=?6)

????{

printf("主菜单:1添加员工?2查看员工列表?3修改员工信息?4查找员工?5排序?6退出程序\n");

scanf("%s",key);

m?=?caidan(1,?6);

????????switch(m)

????????{

????????case?1:

????????????do

{

add();

printf("添加员工成功,是否继续添加(y?/?n)\n");

scanf("%s",?key1);

while(strlen(key1)??1?||?(key1[0]!=?'y'??key1[0]?!=?'n'))

{

printf("输入错误,请重新输入!\n");

scanf("%s",?key1);

}

????????????}

while(key1[0]?!=?'n');

????????????break;

????????case?2:

????????????chakan();

????????????break;

????????case?3:

????????????xiugai();

????????????break;

????????case?4:

????????????chazhao();

????????????break;

????????case?5:

????????????paixu();

????????????break;

????????}

????}

}

TableSort最新版如何支持中文汉字排序

有textSorter配置,配置这个就好了,中文需要使用localeCompare,不能ab这样比

参考:jQuery.tablesorter 2.0中文排序解决办法

$("table").tablesorter({

theme: 'blue', textSorter: function (a, b, asc, table) {

return a.localeCompare(b);

}

});

c语言怎么用姓名(汉字)进行排序

写出姓氏与第一个字母的映射表,比如

赵-Z;

钱-Q;

孙-S;

李-L;

周-Z;

吴-W;

郑-Z;

王-W

然后需要用到排序的时候,再调用它对应的字母来进行排序,也就是比较ASCII码的大小了。编码就自己去实现啦!

Java中如何实现中文排序

public?class?SortByChinese?{

//测试

????public?static?void?main(String[]?args)?{

????????String[]?a1?=?{"保定","何子安","于小云","刘帝","刘何保定子安","刘","刘娜","家强","刘博"};?

????????getSortOfChinese(a1);

????????for?(int?i?=?0;?i??a1.length;?i++)?{

????????????System.out.println(a1[i]);

????????}

????}

/**

?*?

?*?@param?a?String[]

?*?@return?String[]

?*/

????public?static?String[]?getSortOfChinese(String[]?a)?{

????????//?Collator?类是用来执行区分语言环境这里使用CHINA

????????Comparator?cmp?=?Collator.getInstance(java.util.Locale.CHINA);

????????//?JDKz自带对数组进行排序。

????????Arrays.sort(a,?cmp);

????????return?a;

????}

}public?class?SortByChinese?{

//测试

????public?static?void?main(String[]?args)?{

????????String[]?a1?=?{"小二","张三","李四","王五","刘七"};?

????????getSortOfChinese(a1);

????????for?(int?i?=?0;?i??a1.length;?i++)?{

????????????System.out.println(a1[i]);

????????}

????}

/**

?*?

?*?@param?a?String[]

?*?@return?String[]

?*/

????public?static?String[]?getSortOfChinese(String[]?a)?{

????????//?Collator?类是用来执行区分语言环境这里使用CHINA

????????Comparator?cmp?=?Collator.getInstance(java.util.Locale.CHINA);

????????//?Arrays工具类对数组进行排序。

????????Arrays.sort(a,?cmp);

????????return?a;

????}

}

试试对你的问题是否有用

(责任编辑:IT教学网)

更多