Arrays.sort 사용자 정의에 대한 자습서와 많은 인터넷 검색을 읽었지만 답변이 누락되었습니다. 지금 Arrays.sort (charList)는 아무것도하지 않습니다. 내 코드는 다음과 같습니다.Java에서 정렬되지 않은 사용자 정의 된 객체 배열 정렬
public class character implements Comparable<character>{
//public vars
public String charName;
public int initModifier;
public int initRoll;
public int secondInit;
/* ... getters, setters, other vars .. */
@Override
public int compareTo(character another) {
int compareInit = ((character) another).getTotalInit();
int comp = this.totalInit - compareInit;
int compareSecondInit = ((character) another).getTotalInit();
if (comp != 0)
{
return comp;
}
else
{
return this.secondInit - compareSecondInit;
}
}
}
주 활동은 물건 묶음입니다. arrays.sort를 다루는 부분은 다음과 같습니다.
//add a character to the array.
public void addResults(character c)
{
debugInt++; //using this to debug
if(debugInt==3)
{
Log.d(tag,charList[0].charName); //always prints the first object entered
Log.d(tag,charList[1].charName); //always prints the second object entered
}
if (playersPerTurn<charLimit)
{
charList[playersPerTurn]=c;
Arrays.sort(charList,0,playersPerTurn);
playersPerTurn++;
updateDisplay();
}
}
도와주세요. 내 유일한 희망입니다.
몇 가지 '문자'를 생성하고 비교 한 결과를 인쇄하여 compareTo()를 테스트 해 보셨습니까? –
아이디어를 제공해 주셔서 감사합니다. 내가로드 한 데이터는 거기에 답을주지 못했지만이 테스트를 고려하지 않았습니다. – Billdr