【发布时间】:2017-07-16 01:53:41
【问题描述】:
所以我有一个对象列表,我想根据对象的每个名称的特征(按字母顺序)进行排序。我们需要按字母顺序“大致”,所以我有我认为是插入排序算法的平庸实现。但是,我只能按字母顺序正确排序一项。我已经在这里待了好几个小时,似乎遇到了障碍。
public void sort(){
int i=1;
while(list[i]!=null) { //while there is an element in the array
String one=list[i-1].getName(); //get the name of the "first object"
String two=list[i].getName(); //get the name of the "second object"
if(two.compareTo(one)==0){ // if equal move on
i++;
}
else if(two.compareTo(one)<0){// two is before one
Contact temp =list[i-1]; //store this temporarily
list[i-1]=list[i]; //swap them
list[i]=temp; //put temp back where it belongs
i++; //check next elements
}
i++
}
}
这里是什么是列表,在排序之前和之后...http://image.prntscr.com/image/698cf44309ee43c29532ebe71a4925fe.png
【问题讨论】:
-
我真的难倒堆栈溢出社区吗:D O_o
-
在某种程度上,我想你做到了。你在问题中留下了关键信息,让每个人都猜到了,而 Stack Overflow 社区的一些人猜错了。如果你认为这会阻碍社区,我无法阻止你。
标签: java sorting compareto insertion