help with sort

So I have a class called Token and a class called TokenArray. I have a sort function to sort an array of Tokens. I'm pretty new to java so I assumed a sort function would work the same as in C but for some reason it deletes the data instead of switching them. Here is my function:

publicvoid sortTokens(){

boolean swapped =false;

Token temp;

do{

for(int i = 0; i < m_numTokens-1; i++){

if(m_tokenArray[i].compareToIgnoreCase(m_tokenArray[i+1]) > 0){

temp = m_tokenArray[i];

m_tokenArray[i] = m_tokenArray[i+1];

m_tokenArray[i+1] = temp;

swapped =true;

}

}

}while (swapped);

}

Can someone tell me why this isn't working?

null

Message was edited by:

java_dres

[1327 byte] By [java_dresa] at [2007-11-26 19:44:13]
# 1
Can you explain what "deletes the data instead" means.Also you have a potential endless loop as swapped never gets reset to false.
floundera at 2007-7-9 22:28:12 > top of Java-index,Java Essentials,Java Programming...
# 2

No, you have to tell us why it is not working. Then we might be able to tell you what the problem is. Do you get errors when you compile it? Or when you run it? Or does it do something that you didn't expect?

If so, tell us about those errors, expectations, what happened, and so on.

Personally I would not write any sorting code at all, given the existence of the Arrays.sort() method. Unless this is homework and you aren't allowed to use that.

DrClapa at 2007-7-9 22:28:12 > top of Java-index,Java Essentials,Java Programming...
# 3
Ok thanks for the help. I forgot to reset swapped to false and it was running in an infinite loop. The problem with my compiler is that its hard to tell whether the function has finished running or not so I assumed it was messing up the data. And yes, this is for an assignment.
java_dresa at 2007-7-9 22:28:12 > top of Java-index,Java Essentials,Java Programming...
# 4
And thanks for letting me know that theres already a sort function. Sorting is my least favorite part of programming, what with so many different ways to do it.
java_dresa at 2007-7-9 22:28:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> Ok thanks for the help. I forgot to reset swapped to

> false and it was running in an infinite loop. The

> problem with my compiler is that its hard to tell

> whether the function has finished running or not so I

> assumed it was messing up the data. And yes, this is

> for an assignment.

Does this mean you have it working now?

floundera at 2007-7-9 22:28:12 > top of Java-index,Java Essentials,Java Programming...