Connecting Tech Pros Worldwide Help | Site Map

help with sorting an array..

Newbie
 
Join Date: Nov 2007
Posts: 1
#1: Nov 29 '07
hello everybody.. im new here and new to java :)
please help with your expertise

i need to sort an array of string objects..by lexicographical order.
by method compareTo() only.

cant use array.sort() and such.
if i have several words in the array. and i need to enter a new word that is lexicographicaly lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
i tried bubblesort and it didn\t help..

plz show me an example of sorting from this kind.
forever grateful





[code]
public boolean addWord(Word newWord){

boolean work = true;
if (_dic[firstEntry] == null){
_dic[firstEntry] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;

//firstEntry++;}

for (;move<firstEntry;move++)
if (_dic[move].getEnglishWord().compareTo(newWord.getEnglishWord ())<0){
firstEntry++;
//_dic[move] =_dic[move+1];
_dic[firstEntry-11] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;}
work = true;

}
return work;
}
[/CODE
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 29 '07

re: help with sorting an array..


Quote:

Originally Posted by PillarOfCreation

hello everybody.. im new here and new to java :)
please help with your expertise

i need to sort an array of string objects..by lexicographical order.
by method compareTo() only.

cant use array.sort() and such.
if i have several words in the array. and i need to enter a new word that is lexicographicaly lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
i tried bubblesort and it didn\t help..

plz show me an example of sorting from this kind.
forever grateful





[code]
public boolean addWord(Word newWord){

boolean work = true;
if (_dic[firstEntry] == null){
_dic[firstEntry] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;

//firstEntry++;}

for (;move<firstEntry;move++)
if (_dic[move].getEnglishWord().compareTo(newWord.getEnglishWord ())<0){
firstEntry++;
//_dic[move] =_dic[move+1];
_dic[firstEntry-11] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;}
work = true;

}
return work;
}
[/CODE

How about writing down your algorithm first?
If you can't get it right, read about it here for bubble sort.
If you you want a better method then this article is your friend.
Newbie
 
Join Date: Nov 2007
Location: Miami, FL
Posts: 5
#3: Nov 29 '07

re: help with sorting an array..


Quote:

Originally Posted by PillarOfCreation

hello everybody.. im new here and new to java :)
please help with your expertise

i need to sort an array of string objects..by lexicographical order.
by method compareTo() only.

cant use array.sort() and such.
if i have several words in the array. and i need to enter a new word that is lexicographicaly lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
i tried bubblesort and it didn\t help..

plz show me an example of sorting from this kind.
forever grateful





[code]
public boolean addWord(Word newWord){

boolean work = true;
if (_dic[firstEntry] == null){
_dic[firstEntry] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;

//firstEntry++;}

for (;move<firstEntry;move++)
if (_dic[move].getEnglishWord().compareTo(newWord.getEnglishWord ())<0){
firstEntry++;
//_dic[move] =_dic[move+1];
_dic[firstEntry-11] = new Word (newWord.getHebrewWord(),newWord.getEnglishWord()) ;}
work = true;

}
return work;
}
[/CODE

To be honest, I don't know what you mean by lexicographical order. Do you mean in alphabetic order? (Sorry, my first language is not English)

Anyway, you could try sorting them with the Search Sort (I believe its called like that).

And, to move something up or down, you just use yourArray.set();

You will need two loops for this. The inside one is the insertion loop, and the outside one is the one that controls how many times it will run (size-1) times.

list.set(j+1, list.get(j))

I will do the example with numbers.

Imagine you have in your array the numbers:

2 5 4 6

It takes 4, compares it to five, if its bigger does nothing. Since its not, it sets 5 to the next position, and then checks if 4 is bigger than 2, since its not, its ordered. And so on.

May help, may not, I tried :p
Reply