Zerntrino
Zerntrino

Reputation: 67

How to sorting two string arrays

I have two arrays:

arrayA = {"b","c","a"} 
arrayB = {"99","11","22"}

How do I sort them together so that arrayA = {"a","b","c"} and arrayB = {"22","99","11"}?

Upvotes: 1

Views: 1833

Answers (6)

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

I have made one alphanumeric sorting programm Check it out.

import java.util.Arrays;
import java.util.Comparator;

public class AlphanumericSorting implements Comparator {
    public int compare(Object firstObjToCompare, Object secondObjToCompare) {
        String firstString = firstObjToCompare.toString();
        String secondString = secondObjToCompare.toString();

        if (secondString == null || firstString == null) {
            return 0;
        }

        int lengthFirstStr = firstString.length();
        int lengthSecondStr = secondString.length();

        int index1 = 0;
        int index2 = 0;

        while (index1 < lengthFirstStr && index2 < lengthSecondStr) {
            char ch1 = firstString.charAt(index1);
            char ch2 = secondString.charAt(index2);

            char[] space1 = new char[lengthFirstStr];
            char[] space2 = new char[lengthSecondStr];

            int loc1 = 0;
            int loc2 = 0;

            do {
                space1[loc1++] = ch1;
                index1++;

                if (index1 < lengthFirstStr) {
                    ch1 = firstString.charAt(index1);
                } else {
                    break;
                }
            } while (Character.isDigit(ch1) == Character.isDigit(space1[0]));

            do {
                space2[loc2++] = ch2;
                index2++;

                if (index2 < lengthSecondStr) {
                    ch2 = secondString.charAt(index2);
                } else {
                    break;
                }
            } while (Character.isDigit(ch2) == Character.isDigit(space2[0]));

            String str1 = new String(space1);
            String str2 = new String(space2);

            int result;

            if (Character.isDigit(space1[0]) && Character.isDigit(space2[0])) {
                Integer firstNumberToCompare = new Integer(Integer
                        .parseInt(str1.trim()));
                Integer secondNumberToCompare = new Integer(Integer
                        .parseInt(str2.trim()));
                result = firstNumberToCompare.compareTo(secondNumberToCompare);
            } else {
                result = str1.compareTo(str2);
            }

            if (result != 0) {
                return result;
            }
        }
        return lengthFirstStr - lengthSecondStr;
    }

    public static void main(String[] args) {
        String[] alphaNumericStringArray = new String[] { "NUM10071",
                "NUM9999", "9997", "9998", "9996", "9996F" };

        Arrays.sort(alphaNumericStringArray, new AlphanumericSorting());

        for (int i = 0; i < alphaNumericStringArray.length; i++) {
            System.out.println(alphaNumericStringArray[i]);
        }

    }

}

Here is the output:

9996 9996F 9997 9998 NUM9999 NUM10071

Upvotes: 1

Shehzad
Shehzad

Reputation: 2940

you can use

Arrays.sort(arrayB);

Output is

C1
C2
C3



  arrayB = {"22","99","11"}
  Arrays.sort(arrayB);

Output:

11
22
99

Upvotes: 0

Stephen C
Stephen C

Reputation: 718698

The answer to the question as written seems so obvious that I think we are not understanding what you are really asking.

I'm guessing that what you are really asking is how to sort one array, and reorder the second array based on how the sort reordered the first array. (Your example is poorly chosen to illustrate this ... but it does in fact doing this.)

Assuming that's what you mean, the simplest way to do this is to turn the two arrays into a single array of pairs, sort the pairs based on the first field, and then use the sorted pair list to repopulate the original arrays.

The (possible) snag is that the total ordering of the second array is indeterminate if the sort is not stable. (Or to put it another way, if there are duplicates in arrayA, then the relative order of the corresponding arrayB elements cannot be predicted.) There are ways to deal with this.

Upvotes: 0

npinti
npinti

Reputation: 52185

If you will be using the same letters and only 1 digit from 0 to 9, then, you can sort it in the same manner you have sorted the first array. If you intend to throw in more letters with more numbers, you will have to implement your own custom comparator (assuming the default behaviour does not suit you).

Upvotes: 0

AlexR
AlexR

Reputation: 115328

Use Arrays.sort(arrayB). You can even supply your custom Comparator to affect the sorting.

Upvotes: 1

Ibraheem Osama
Ibraheem Osama

Reputation: 334

This link will help you Try array.sort(Arg); http://www.leepoint.net/notes-java/data/arrays/70sorting.html

Upvotes: 0

Related Questions