0

Example:

Input: { 1,22,44,3456,9999}
Output {44,9999,3456,22}

What i did was put integer,String values in HashMap.(map.put(0,"zero") etc..)

Create a new String array using above HashMap

String Array{ one,twotwo,fourfour,threefourfivesix,ninenineninenine}

remember the indexes of input to String Array

Sort the String Array and then replace the String values with Integers using HashMap.

Is there any other better way for doing this??

package l337;

import java.util.Arrays;
import java.util.HashMap;

public class temp{

public static void main(String[] arg){

    HashMap<Integer,String> map = new HashMap<Integer,String>();
    map.put(0,"zero");
    map.put(1,"one");
    map.put(2,"two");
    map.put(3,"three");
    map.put(4,"four");
    map.put(5,"five");
    map.put(6,"six");
    map.put(7,"seven");
    map.put(8,"eight");
    map.put(9,"nine");

    int[] input ={1,2,44,66,7895,88983};
    String[] stringArray = new String[input.length];

    for(int i=0;i<input.length;i++){

        stringArray[i] = map.get(input[i]);
        if(stringArray[i]==null){
            char[] temp= (new Integer(input[i])).toString().toCharArray();
            for(int j=0;j<temp.length;j++){
                stringArray[i]+=map.get((int)temp[j]-48);
            }
        }
    }
    HashMap<String,Integer> maploc = new HashMap<String,Integer>();
    map.put(0,"zero");
    for(int i=0;i<input.length;i++){
        maploc.put(stringArray[i], input[i]);
    }

    Arrays.sort(stringArray);

    int[] output =new int[input.length];
    for(int i=0;i<input.length;i++){
        output[i]= maploc.get(stringArray[i]);
    }
     for(int i:input){

        System.out.print(i+" ");
    }
     System.out.println();
    for(int i:output){

        System.out.print(i+" ");
    }

    }


}
eagle06
  • 3
  • 3
  • what u coded till nw ? – Kick Feb 10 '14 at 09:13
  • You may have to be a bit more explicit with what you mean by "etc" in `map.put(0,"zero") etc.`, because I'm assuming you didn't hard-code 10000 values. – Bernhard Barker Feb 10 '14 at 09:14
  • Btw, you map should be built the other way around: `map.put("zero", 0)`. – Cedric Reichenbach Feb 10 '14 at 09:15
  • You could just make a Comparator for Integer and sort with your custom Comparator. – Alowaniak Feb 10 '14 at 09:16
  • Yes, there is a better way, you could create an object with two variables, the number and the string. Than you could create an array of that class and sort it using a proper comparator. But... this seems to be a homework, so follow the way suggested by teacher. – holap Feb 10 '14 at 09:16
  • 1
    Possible duplicate of [How to convert number to words in java](http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java) (from there, just sort based on the resulting string). – Bernhard Barker Feb 10 '14 at 09:18
  • Sounds like you should implement a method that converts an `int` into a string of its English-language representation. Sorting once you have that is pretty trivial. – dlev Feb 10 '14 at 09:19

3 Answers3

0

The best way to do this is create a single object that has the number and the string representation.

i.e. when you create a new NumberAndName(3) it will set an internal integer to 3 and internal string to "three". How to handle that mapping is up to you but if you just need the numbers 0 to 9 a simple array of Strings would work.

You can then just use Collections.sort() on a collection of NumberAndName objects, specifying a comparator that uses the name to compare them.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

When you already have a HashMap storing the mapping, you can just use a Comparator that compares the entries of the array by looking up and comparing the respective values in the map

import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;

public class SortByName
{
    public static void main(String[] args)
    {
        Integer input[] = { 1,22,44,3456,9999};
        final String array[] = { 
            "one", "twotwo", "fourfour", "threefourfivesix", 
            "ninenineninenine"};

        final Map<Integer, String> map = createMap(input, array);
        Arrays.sort(input, new Comparator<Integer>()
        {
            @Override
            public int compare(Integer i0, Integer i1)
            {
                return map.get(i0).compareTo(map.get(i1));
            }

        });
        System.out.println(Arrays.toString(input));
    }

    private static <K, V> Map<K, V> createMap(K k[], V v[])
    {
        Map<K, V> map = new LinkedHashMap<K, V>();
        for (int i=0; i<k.length; i++)
        {
            map.put(k[i], v[i]);
        }
        return map;
    }
}
Marco13
  • 53,703
  • 9
  • 80
  • 159
0

You could use enum to represent digits and have lookups (and reverse lookup), helper functions to convert to/from number-string. This is a quick dirty implementation that does this.

public static void arrayEnglistTest()
{
    int[] array = { 1, 22, 44, 3456, 9999 };

    String[] stringArray = new String[array.length];
    int idx = 0;
    for (int num : array)
    {
        stringArray[idx++] = getNumberInEnglish(num);
    }

    Arrays.sort(stringArray);

    idx = 0;
    for (String stringNumber : stringArray)
    {
        array[idx++] = getNumber(stringNumber);
    }
}

public static int getNumber(String numberInEnglish)
{
    StringBuilder result = new StringBuilder();
    StringBuilder builder = new StringBuilder();
    for (char c : numberInEnglish.toCharArray())
    {
        builder.append(c);
        String soFar = builder.toString();
        if (DIGIT.getNumber(soFar) != null)
        {
            result.append(DIGIT.getNumber(soFar));
            builder.setLength(0);
        }
    }
    return Integer.parseInt(result.toString());
}

public static String getNumberInEnglish(int number)
{
    StringBuilder builder = new StringBuilder();
    while (number > 0)
    {
        builder.insert(0, DIGIT.getNumberInEnglish(number % 10));
        number /= 10;
    }
    return builder.toString();
}

enum DIGIT
{
    ONE(1, "one"), TWO(2, "two"), THREE(3, "three"), FOUR(4, "four"), FIVE(5, "five"), SIX(6, "six"), SEVEN(7,
            "seven"), EIGHT(8, "eight"), NINE(9, "nine"), ZERO(0, "zero");

    private DIGIT(int digit, String digitInEnglish)
    {
        this.digit = digit;
        this.digitInEnglish = digitInEnglish;
    }

    public static String getNumberInEnglish(int number)
    {
        return map.get(number);
    }

    public static Integer getNumber(String numberInEnglish)
    {
        return reverseMap.get(numberInEnglish);
    }

    int digit;
    String digitInEnglish;

    static Map<Integer,String> map = new HashMap<Integer,String>() {
        {
            for (DIGIT dig : EnumSet.allOf(DIGIT.class))
            {
                put(dig.digit, dig.digitInEnglish);
            }
        }
    };
    static Map<String,Integer> reverseMap = new HashMap<String,Integer>() {
        {
            for (DIGIT dig : EnumSet.allOf(DIGIT.class))
            {
                put(dig.digitInEnglish, dig.digit);
            }
        }
    };
}
aNish
  • 1,079
  • 6
  • 11