面试题题排序,字符串数组按照字母排序,不区分大小写比较

时间:2023-01-16 00:52:48

面试题题排序,字符串数组按照字母排序,不区分大小写比较

对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“Bc”,“Ad”,“aC”,“Hello”,“X man”,“little”,“During”,“day”能够排序成 “Ad”,”aC”,“Bc”,“During”,“day”,“Hello”,“little”,“Hello”,也就是说,在排序的过程并不是传统的 按照字符串排序,在排序中还需要将小写字母一并排序,也就是说a字符串要在B或b之前。

类库方法

public static void main(String[] args) {

String source[] = { "dad", "bood", "bada", "Admin", "Aa ", "A ", "Good", "aete", "cc", "Ko", "Beta", "Could" };

List<String> list = Arrays.asList(source);

//String.CASE_INSENSITIVE_ORDER A在 a 前面
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println(list);
}/**
*output
[A , Aa , Admin, aete, bada, Beta, bood, cc, Could, dad, Good, Ko]
*/

java 实现

我用的冒泡排序,其实就是compare(String first, String second)这儿需要自己写,其中还有对于不同长度的字符串怎么处理
转换 大小写
当然你也可以自己建个类,实现Comparable接口


public class MyString implements Comparable<MyString> {
private String string;
...
//比较
public int compareTo(MyString other) {
...
}
}

先比较第1个字符相等,若等于,比较下一个。若是小写字母,要转化为大写字母,因为 a 的 ascii 码比 A–Z 都大

static void sort(String[] data) {
for (int i = 0; i < data.length - 1; i++) {
for (int j = data.length - 1; j > i; j--) {
if (compare(data[j], data[j - 1]) < 0) {
String temp = data[j];
data[j] = data[j - 1];
data[j - 1] = temp;
}
}
}
}

static int compare(String first, String second) {
int length = first.length() < second.length() ? first.length() : second.length();
int mark = 0xffdf; // 1...1,1101,1111,转换第6
for (int i = 0; i < length; i++) {

char f = first.charAt(i);
char s = second.charAt(i);
if (f == s) {
continue;
}
// 这儿小写转大写
// 注意空格的情况
//空格会在大小写字母前面
f = (char) (f & mark);
s = (char) (s & mark);
System.out.println(first.charAt(i) + "->" + f + " " + second.charAt(i) + "->" + s);
// f = (f < 'a' && f >= 'A') ? f : (char) (f - 'a' + 'A');
// s = (s < 'a' && s >= 'A') ? s : (char) (s - 'a' + 'A');

if (f < s) {
return -1;
} else if (f > s) {
return 1;
} else {
// 下面的代码注意,大于返回-1.小于返回1,a 在 A 前面,b 在 B 前面,小写字母排在在大写字母前面
//自己的方法是把 a 排在 A 前面,和类库相反
return first.charAt(i) > second.charAt(i) ? -1 : 1;
}
}

return first.length() < second.length() ? -1 : first.length() > second.length() ? 1 : 0;
}

大小写转换的方法

A ascii 为 65, a 为 97

A 65 0100,0001
a 97 0110,0001–>a的第6位转为0,就能变为A

ch = ch | 32 –> 大写转小写,把第6位, 置为1
ch = ch & 0xdf –> 小写转大写,把第6位, 置为0

int mark = 0xffdf; // 1...1,1101,1111,转换第6位
// 这儿小写转大写
// 注意空格的情况
f = (char) (f & mark);
s = (char) (s & mark);

code 结果

    public static void main(String[] args) {

String source[] = { "dad", "bood", "bada", "Admin", "Aa ", "A ", "Good", "aete", "cc", "Ko", "Beta", "Could" };

List<String> list = Arrays.asList(source);

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println(list);

System.out.println("-------------------------------");
sort(source);
System.out.println(Arrays.toString(source));

}/**
*output
[A , Aa , Admin, aete, bada, Beta, bood, cc, Could, dad, Good, Ko]
-------------------------------
// 自己的方法是把 a 排在 A 前面,和类库相反
[aete, A , Aa , Admin, bada, bood, Beta, cc, Could, dad, Good, Ko]
*/

自己的方法是把 a 排在 A 前面,和类库相反

如文章对您有帮助,请多多关照

面试题题排序,字符串数组按照字母排序,不区分大小写比较