Java Vector Sort sort(Vector unsorted, int sortKeyPos, int maxVal)

Here you can find the source of sort(Vector unsorted, int sortKeyPos, int maxVal)

Description

Sorts the elements of a Vector.

License

Open Source License

Parameter

Parameter Description
unsorted unsorted vector. Each element of the vector should an object array of atleast single dimension. One element of the object array should be of type int.
sortKeyPos The position of the sort key in the Object array.
maxVal The maximum value of the sort key.

Declaration

public static Vector sort(Vector unsorted, int sortKeyPos, int maxVal) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**/*from   w  ww . ja  v  a  2s.  co m*/
    * Sorts the elements of a Vector. Each element of the vector is an array of objects.
    * One of the elements of this object array must be an integer. This element is
    * called the sort key. The elements of the vector will be sorted in ascending order
    * of the sort key.
    * The sort key values must be unique and greater than 0.
    *
    * @param  unsorted    unsorted vector. Each element of the vector should an object
    * array of atleast single dimension. One element of the object array should
    * be of type int.
    * @param  sortKeyPos  The position of the sort key in the Object array.
    * @param  maxVal      The maximum value of the sort key.
    */
    public static Vector sort(Vector unsorted, int sortKeyPos, int maxVal) {

        int listSize = unsorted.size();
        Vector sorted = (Vector) unsorted.clone();

        int[] map = new int[maxVal];

        int index = -1;
        for (int i = 0; i < listSize; i++) {
            index = ((Integer) (((Object[]) unsorted.elementAt(i))[sortKeyPos])).intValue();
            if (index > maxVal)
                throw new IllegalArgumentException("Incorrect maxVal");
            map[--index] = (i + 1);
        }

        for (int i = 0, j = 0; i < listSize; i++, j++) {
            while (map[j] == 0)
                j++;
            sorted.setElementAt(unsorted.elementAt((map[j] - 1)), i);
        }

        return sorted;
    }

    /**
    * Sorts the elements of a Vector. Each element of the vector is an array of objects.
    * One of the elements of this object array must be an integer. This element is
    * called the sort key. The elements of the vector will be sorted in ascending order
    * of the sort key.
    * The sort key values must be unique and greater than 0.
    *
    * @param  unsorted    unsorted vector. Each element of the vector should an object
    * array of atleast single dimension. One element of the object array should
    * be of type int.
    * @param  sortKeyPos  The position of the sort key in the Object array.
    * @param  minVal      The minimum value of the sort key.
    * @param  maxVal      The maximum value of the sort key.
    */
    public static Vector sort(Vector unsorted, int sortKeyPos, int minVal, int maxVal) {

        int listSize = unsorted.size();
        Vector sorted = (Vector) unsorted.clone();

        if (minVal < 1)
            throw new IllegalArgumentException("Invalid minVal, must be greater than zero");

        int[] map = new int[(maxVal - minVal + 1)];

        if (map.length < listSize)
            throw new IllegalArgumentException("Invalid data vector, check values");

        int index = -1;
        for (int i = 0; i < listSize; i++) {
            index = ((Integer) (((Object[]) unsorted.elementAt(i))[sortKeyPos])).intValue() - minVal;

            if (index > (map.length - 1))
                throw new IllegalArgumentException("Error: Please check minVal and maxVal settings");

            map[index] = (i + 1);
        }

        for (int i = 0, j = 0; i < listSize; i++, j++) {
            while (map[j] == 0)
                j++;
            sorted.setElementAt(unsorted.elementAt((map[j] - 1)), i);
        }

        return sorted;
    }
}

Related

  1. sortInsert(Vector v, Object obj)
  2. sortIntegerVector(Vector toSort)