0

I am looking to print out my original unsorted array, I have it printing in order and sorted but I can't seem to get the original one to print out unsorted. I have used printRuleAndArray(String rule) and I have also used LengthCompare for the new sorted array, my problem is the original!!!

import java.util.*;
import java.util.Arrays;

// Example of how to sort an array
public class Sorting2
{
    //declare an array of strings
    static String[] nameArray = {"Alan", "Peter", "Ed", "Stephen", "Pheadraa"};

    public static void main(String[] args)
    {

        // sorting by length
        Arrays.sort(nameArray, new LengthCompare());
        //print out elements of array
        System.out.println(Arrays.toString(nameArray));
        //count the number of elements in the array
        int counter=nameArray.length;
        //print out numeric number of elements in array
        System.out.println("Number of elements in array: " + counter);
        //print out sorted array with shortest first and longest last
        printRuleAndArray("Sorted list by name length:");

    }
3
  • 5
    You're sorting the array, and you want to print it unsorted? Conflict of interest.. Commented Mar 18, 2014 at 10:49
  • I need to see the unsorted array to compare the two Commented Mar 18, 2014 at 10:56
  • Then create two arrays, sort one and don't change the other. Commented Mar 18, 2014 at 11:03

6 Answers 6

3

Arrays.sort() will always sort the array you pass into it, it doesn't produce a fresh copy - so if you really need the unsorted array to hang around as well as the sorted array, then you'll have to make a copy of it:

String copyArr[] = new String[nameArray.length];
System.arraycopy( nameArray, 0, copyArr, 0, nameArray.length );

However, preferable to this approach (if feasible) would just be to do all the operations you need on the unsorted array (such as printing it or converting it to a string), then sort it afterwards.

As pointed out in the comment, Arrays.copyOf() could also be used to accomplish the same thing.

Sign up to request clarification or add additional context in comments.

1 Comment

There is Arrays.copyOf() to copy arrays, it is more simple than using System.arraycopy()
1

Arrays.sort will have altered your original array. Your choices are to either print your original array before sorting it, or to copy your original array and sort the copy.

Comments

0

Call String unsortedArr = Arrays.toString(nameArray); before array sorting, and when you need to print unsorted array just call System.out.println(unsortedArr);

Comments

0

Arrays.sort() sorts the array you pass into it. If you would like the original array later, copy the array first and then sort that array.

Comments

0
 for(String arr : nameArray ) {  //arr gets successively each value in nameArray.
System.out.println(arr);
}

this example is using foreach loop

Comments

0

Do something like this

    String orig = Arrays.toString(nameArray);
    Arrays.sort(nameArray, new LengthCompare());
    String sorted = Arrays.toString(nameArray);

    System.out.println(orig);
    System.out.println(sorted);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.