aspose file tools*
The moose likes Beginning Java and the fly likes sorting an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting an array" Watch "sorting an array" New topic
Author

sorting an array

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi,
I have an array of Strings

I use this data to build a tree. Before I build the tree, I want to sort the data into alphabetical order of column 2. Is this easy ?
Cheers,
Kate
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Take a look of this example: ( from Java How to Program? chater 7)
import java.awt.*;
import javax.swing.*;
public class BubbleSort extends JApplet {
public void init()
{
JTextArea outputArea = new JTextArea();
Container c = getContentPane();
c.add( outputArea );
int a[] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
String output = "Data items in original order\n";
for ( int i = 0; i < a.length; i++ )
output += " " + a[ i ];
bubbleSort( a );
output += "\n\nData items in ascending order\n";
for ( int i = 0; i < a.length; i++ )
output += " " + a[ i ];
outputArea.setText( output );
}
// sort the elements of an array with bubble sort
public void bubbleSort( int b[] )
{
for ( int pass = 1; pass < b.length; pass++ ) // passes
for ( int i = 0; i < b.length - 1; i++ ) // one pass
if ( b[ i ] > b[ i + 1 ] ) // one comparison
swap( b, i, i + 1 ); // one swap
}
// swap two elements of an array
public void swap( int c[], int first, int second )
{
int hold; // temporary holding area for swap
hold = c[ first ];
c[ first ] = c[ second ];
c[ second ] = hold;
}
}
**Hope this helps! Let me know if you have more question.
Happy Programmer!
Mindy
[This message has been edited by Mindy Wu (edited June 12, 2001).]
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Thankyou, that worked great.
Kate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting an array
 
Similar Threads
I think Im almost there with the tree..please let mw know what you think
Tree Implementation using Trinidad tag
dynamic tree structure with struts
Ejb cache with singelton implementation
Making binary tree: memory problem