All Packages Class Hierarchy This Package Previous Next Index
Sorting1 - Sorting a Vector, a java.util.Vector, and a native array of primitives.
Sorting2 - Sorting a primitive array without iterators.
Sorting3 - Changing the order of traversal for containers.
Sorting1 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;
import java.util.Vector;
/**
* Sorting a Vector, a java.util.Vector, and a native array of primitives.
*
* @see com.objectspace.jgl.algorithms.Sorting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Sorting1
{
public static void main( String[] args )
{
System.out.println( "Sort an Array of Integers" );
Array array = new Array();
array.add( new Integer( 7 ) );
array.add( new Integer( 10 ) );
array.add( new Integer( 3 ) );
array.add( new Integer( -4 ) );
System.out.println( "unsorted = " + array );
Sorting.sort( array );
System.out.println( "ascending = " + array );
Sorting.sort( array, new GreaterNumber() );
System.out.println( "descending = " + array );
System.out.println();
System.out.println( "Sort a java.util.Vector of Strings" );
Vector vector = new Vector();
vector.addElement( "dog" );
vector.addElement( "ape" );
vector.addElement( "fox" );
vector.addElement( "bat" );
VectorArray vectorArray = new VectorArray( vector );
System.out.println( "unsorted = " + vectorArray );
Sorting.sort( vectorArray, new LessString() );
System.out.println( "ascending = " + vectorArray );
System.out.println();
System.out.println( "Sort a primitive array of ints" );
int ints[] = { 3, 6, 1, 2, 9, 8, 1, 8 };
IntArray intArray = new IntArray( ints );
System.out.println( "unsorted = " + intArray );
Sorting.sort( intArray, new GreaterNumber() );
System.out.println( "descending = " + intArray );
ForwardIterator start = intArray.start();
ForwardIterator finish = intArray.finish();
start.advance(3);
Sorting.sort( start, finish );
System.out.println( "partially ascending = " + intArray );
}
}
Sort an Array of Integers
unsorted = Array( 7, 10, 3, -4 )
ascending = Array( -4, 3, 7, 10 )
descending = Array( 10, 7, 3, -4 )
Sort a java.util.Vector of Strings
unsorted = [dog, ape, fox, bat]
ascending = [ape, bat, dog, fox]
Sort a primitive array of ints
unsorted = int[]( 3, 6, 1, 2, 9, 8, 1, 8 )
descending = int[]( 9, 8, 8, 6, 3, 2, 1, 1 )
partially ascending = int[]( 9, 8, 8, 1, 1, 2, 3, 6 )
Sorting2 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import java.util.Vector;
/**
* Sorting a primitive array without iterators.
*
* @see com.objectspace.jgl.algorithms.Sorting
* @version 1.0
* @author ObjectSpace, Inc.
*/
public class Sorting2
{
public static void main( String[] args )
{
System.out.println( "Sort a primitive array of chars" );
char c[] = { 'c', 'z', 'e', 'f', 'g', 'o', 'a' };
CharArray cArray = new CharArray( c );
System.out.println( "unsorted = " + c );
Sorting.sort( cArray );
System.out.println( "sorted = " + c );
}
}
Sort a primitive array of chars
unsorted = czefgoa
sorted = acefgoz
Sorting3 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;
/**
* Changing the order of traversal for containers.
*
* @see com.objectspace.jgl.algorithms.Sorting
* @version 1.0
* @author ObjectSpace, Inc.
*/
public class Sorting3
{
public static void main( String[] args )
{
HashSet s = new HashSet();
s.add( "Austin" );
s.add( "Texas" );
s.add( "Fight" );
s.add( "longhorn" );
s.add( "Bevo" );
// show in the default order
System.out.print( "normal: " );
Printing.println( s.begin(), s.end() );
// show sorted
System.out.print( "less: " );
Range r = Sorting.iterSort( s, new LessString() );
Printing.println( r.begin, r.end );
// show sorted a different way
System.out.print( "greater: " );
r = Sorting.iterSort( s, new GreaterString() );
Printing.println( r.begin, r.end );
// show that iterSort() doesn't sort container
System.out.print( "normal: " );
Printing.println( s.begin(), s.end() );
}
}
normal: ( Texas, Austin, Bevo, Fight, longhorn )
less: ( Austin, Bevo, Fight, Texas, longhorn )
greater: ( longhorn, Texas, Fight, Bevo, Austin )
normal: ( Texas, Austin, Bevo, Fight, longhorn )
All Packages Class Hierarchy This Package Previous Next Index