473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with sorting an array..

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
Nov 29 '07 #1
2 1185
r035198x
13,262 8TB
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.
Nov 29 '07 #2
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
Nov 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
2
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
2
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
1
by: thebrainmore | last post by:
could someone help with my average and sort functions in this program #include <iostream.h> //funtion prototypes int FindLowNum(int , int ); int FindHighNum(int , int);
3
by: iskeletor | last post by:
i want to have integers from user and take them into a array then sorting them but where do i make mistake?thanx from now #include <stdio.h> #include <stdlib.h> int...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.