• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

sorting an array

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou, that worked great.
Kate
reply
    Bookmark Topic Watch Topic
  • New Topic