Java Array Sort sortedArraysEqual(Object[] arr1, Object[] arr2)

Here you can find the source of sortedArraysEqual(Object[] arr1, Object[] arr2)

Description

Determine whether two arrays contain the same elements, even if those elements aren't in the same order in both arrays.

License

Open Source License

Parameter

Parameter Description
arr1 The first array
arr2 The second array

Return

True if the arrays contain the same elements, else false.

Declaration

public static boolean sortedArraysEqual(Object[] arr1, Object[] arr2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Arapiki Solutions Inc.
 * 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
 *
 * Contributors:/*from  w ww.  j a  v a 2  s  . c  o m*/
 *    "Peter Smith <psmith@arapiki.com>" - initial API and 
 *        implementation and/or initial documentation
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * Determine whether two arrays contain the same elements, even if those elements
     * aren't in the same order in both arrays. The equals() method is used to determine
     * if elements are the same.
     * @param arr1 The first array
     * @param arr2 The second array
     * @return True if the arrays contain the same elements, else false.
     */
    public static boolean sortedArraysEqual(Object[] arr1, Object[] arr2) {

        /* if one is null, then both must be null */
        if (arr1 == null) {
            return (arr2 == null);
        }

        /* types must be the same */
        if (arr1.getClass() != arr2.getClass()) {
            return false;
        }

        /* lengths must be the same */
        if (arr1.length != arr2.length) {
            return false;
        }

        /* now sort the elements and compare them */
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }
}

Related

  1. sortCompare(String[] valuesOne, String[] valuesTwo)
  2. sortDesc(long[] keys, int[] values)
  3. sortDescending(T[] a, int fromIndex, int toIndex)
  4. SortDoubleDimensionArray(String StrArr[][])
  5. sorted(int[] array)
  6. sortedCopyOf(final int[] array)
  7. sortedIndex(double[] values)
  8. sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
  9. sortedMerge(int[] a, int[] b)