Java Array Sort sort3(String[] a, int x, int y, int z)

Here you can find the source of sort3(String[] a, int x, int y, int z)

Description

Auxiliary method for sorting lexicographically the strings at the positions x, y and z.

License

Open Source License

Parameter

Parameter Description
a represents the array with the strings for sorting.
x position of the first string.
y position of the second string.
z position of the third string.

Declaration

private static void sort3(String[] a, int x, int y, int z) 

Method Source Code

//package com.java2s;
/**/*from w w w  . j  a  va2s.  co m*/
 * Copyright (c) 1997, 2015 by ProSyst Software GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Auxiliary method for sorting lexicographically the strings at the positions x, y and z.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     * @param z position of the third string.
     */
    private static void sort3(String[] a, int x, int y, int z) {
        if (a[x].compareTo(a[y]) > 0) {
            if (a[x].compareTo(a[z]) > 0) {
                if (a[y].compareTo(a[z]) > 0) {
                    swap(a, x, z);
                } else {
                    swap3(a, x, y, z);
                }
            } else {
                swap(a, x, y);
            }
        } else if (a[x].compareTo(a[z]) > 0) {
            swap3(a, x, z, y);
        } else if (a[y].compareTo(a[z]) > 0) {
            swap(a, y, z);
        }
    }

    /**
     * Auxiliary method for sorting lexicographically the strings. Shuffling strings on positions x and y, as the string
     * at the position x, goes to the position y, the string at the position y, goes to the position x.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     */
    private static void swap(String[] a, int x, int y) {
        String t = a[x];
        a[x] = a[y];
        a[y] = t;
    }

    /**
     * Auxiliary method for sorting lexicographically the strings. Shuffling strings on positions x, y and z, as the
     * string
     * at the position x, goes to the position z, the string at the position y, goes to the position x and the string
     * at the position z, goes to the position y.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     * @param z position of the third string.
     */
    private static void swap3(String[] a, int x, int y, int z) {
        String t = a[x];
        a[x] = a[y];
        a[y] = a[z];
        a[z] = t;
    }
}

Related

  1. sort(T[] src)
  2. sort1(int x[], int off, int len, int y[])
  3. sort1(long x[], int off, int len)
  4. sort2(double v[], int v2[], int left, int right)
  5. sort2(double[] arr, double[] brr)
  6. sort3(String[] arr, float[] brr)
  7. sort5(Long[] data)
  8. sortableBytesToInt(byte[] encoded, int offset)
  9. sortAccording(float[] ary, Integer[] sortOrder)