joe8032
joe8032

Reputation: 11

To print original array not sorted array

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:");

    }

Upvotes: 0

Views: 2151

Answers (6)

chettyharish
chettyharish

Reputation: 1734

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);

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72294

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.

Upvotes: 3

iamsuman
iamsuman

Reputation: 1413

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

this example is using foreach loop

Upvotes: 0

DaveH
DaveH

Reputation: 7335

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.

Upvotes: 1

anirudh
anirudh

Reputation: 4176

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.

Upvotes: 0

alex2410
alex2410

Reputation: 10994

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

Upvotes: 0

Related Questions