All Packages  Class Hierarchy  Previous  Next  Index

Sorting Examples

Sorting 1 - Sorting a Vector, a java.util.Vector, and a native array of primitives.

Sorting 2 - Sorting a primitive array without iterators.


Sorting1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;
import java.util.Vector;

/**
 * Sorting a Vector, a java.util.Vector, and a native array of primitives.
 *
 * @see COM.objectspace.jgl.Sorting
 * @version 2.0.2
 * @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 );
    }
  }

Sorting1 Example Output

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.*;
import java.util.Vector;

/**
 * Sorting a primitive array without iterators.
 *
 * @see COM.objectspace.jgl.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 );
    }
  }

Sorting2 Example Output

Sort a primitive array of chars
unsorted = czefgoa
sorted   = acefgoz

All Packages  Class Hierarchy  Previous  Next  Index