0

Recently I faced a question in an interview and I was unable to make logic for this question. I have an array like

[ 1,'a',45,'h',56,'d',2,'t',6,'p' ] . How to sort this array ? Output should be in this manner..

intArray = [1,2,6,45,56] 
charArray= ['a','d','h','p','t']

If anybody knows its logic please comment. It would be a great help.

Thanks !

4
  • is it Array or ArrayList ? Commented Sep 24, 2015 at 5:09
  • How original array is stored in an array? Commented Sep 24, 2015 at 5:14
  • @Satya, NamanGala, does that matter? It's a question about sorting heterogeneous elements. I don't think the specifics of the underlying data structure matter that much. In all interviews I've done pseudo code has been fine. Commented Sep 24, 2015 at 5:15
  • OP, I guess you could split the array into an intArray and a charArray first, and then sort those two arrays separately. Or, you could create a comparator that primarily sorts on element type and secondarily on value. Then sort the whole thing and split it in two. Commented Sep 24, 2015 at 5:18

4 Answers 4

2

One way would be separate the integers and then sort-

Object[] objects = new Object[]{ 1,'a',45,'h',56,'d',2,'t',6,'p' };

List integers = new ArrayList<Integer>();
List characters = new ArrayList<Character>();

// Check and store integers and characters
// Doesn't validate and assumes you either have integers or characters
for(Object o : objects){
    if(o instanceof Integer){
        integers.add(o);                
    } else {                
        characters.add(o);
    }       
}

//Sort them separately
Collections.sort(integers);
Collections.sort(characters);

System.out.println(integers);
System.out.println(characters);
Sign up to request clarification or add additional context in comments.

Comments

0
  1. First of all separate the integers and character into different arrays by checking its instance type.
  2. If characters are stored in ASCII form (in java it is stored in ASCII form) you can directly sort them using any of the sorting algorithm, treating each value as integer only.
  3. Similarly you can apply any sorting algorithm on integer array.

Comments

0

The following code produces desired result:

import static java.util.stream.Collectors.*;

final Map<Class<?>, Set<Object>> result = Stream.of(array) //
        .collect(//
                groupingBy(x -> x.getClass(), //
                        mapping(x -> x, toCollection(TreeSet::new))));

to view results:

System.out.println(Arrays.toString(result.get(Integer.class).toArray()));
System.out.println(Arrays.toString(result.get(Character.class).toArray()));

Comments

0

Below is the implementation of it. I have used two for loops so that we can easily find out the length of the two new subarrays instead of initializing it with the base array;s length. I have tried not to use any in-built methods except Arrays.sort(). You can also write your own code to sort these two sub arrays.

    public static void main(String[] args) {
    Object[] array = { 1, 'a', 2, 'f', 5, 'b', 3 };

    int intLoc = 0;
    int charLoc = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] instanceof Integer) {
            intLoc++;
        } else {
            charLoc++;
        }
    }
    int intArray[] = new int[intLoc];
    char charArray[] = new char[charLoc];

    for (int i = 0; i < array.length; i++) {
        if (array[i] instanceof Integer) {
            --intLoc;
            intArray[intLoc] = (int) array[i];
        } else {
            --charLoc;
            charArray[charLoc] = (char) array[i];
        }
    }

    Arrays.sort(intArray);
    Arrays.sort(charArray);
    System.out.println(Arrays.toString(intArray));
    System.out.println(Arrays.toString(charArray));

}

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.