sorting and unsorting

Hi everyone,
Im working on a mock prototype encryption program but I have some problem with a certain part of it.
My problem is that I have a string (e.g. "privatekey") which I have to first sort alphabetically to become "aeeikprtvy"
and then number them from 1 to the length of the string in an array of numbers , in this case [1,2,3,4,5,6,7,8,9,10]
after that I have to move them to their normal position in the string, in this case [6,7,4,9,1,8,2,5,3,10]. now the sorting bit is done but im still rather perplexed as to how to return the sorted array of integers to the string's original position.
thanks for your help!
PS: I hope that was clear enough to understand

LVL 3
MytixAsked:
Who is Participating?
 
suprapto45Connect With a Mentor Commented:
Hi,

I am going to give you some codes. Well, it is quite complex but I hope that you can understand it.

/*
 * Created on Apr 15, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.experts;

import java.util.Arrays;

/**
 * @author user
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Sort
{
      static private String test = "privatekey";
      
      public static void Unsort(char[] temp, int[] temp2)
      {
            boolean flag = false;
            
            int status = 0;
            int[] checked = new int[test.length()];
            
            for (int u = 1; u <= checked.length; u++)
            {
                  checked[u - 1] = -1;
            }            
            
            char[] x = new char[test.length()];
            x = test.toCharArray();
            
            for (int j = 0; j < x.length; j++)
            {
                  for (int k = 0; k < temp.length; k++)
                  {
                        if(temp[k] == x[j])
                        {
                              for (int l = 0; l < checked.length; l++)
                              {
                                    if (k == checked[l])
                                    {
                                          flag = true;
                                    }
                              }
                              if (flag == false)
                              {                              
                                    System.out.println(k + 1);
                                    checked[status] = k;      
                                    status++;                                    
                                    break;
                              }
                              else
                              {
                                    flag = false;
                              }
                        }            
                  }
            }
      }

      
      public static void main(String[] args)
      {
            
            char[] a = new char[test.length()];
            int[] b = new int[test.length()];
            
            for (int i = 1; i <= test.length(); i++)
            {
                  b[i - 1] = i;
            }
            
            a = test.toCharArray();
            Arrays.sort(a);
      
            Unsort(a, b);
                        
      }
}

I hope that helps.

Regards
Dave
0
 
suprapto45Commented:
Hi,

It is clear...let me try to help you.

Regards
Dave
0
 
vijaydograCommented:

Is this what expected?

               Hashtable htref = new Hashtable();
            for(i = 0 ;i < str_arrary.length ; i++)
                  htref.put(str_array[i],new Integer(i).toString());
            //sorting part of the string array
            //Take the array now
            for(j = 0 ; j < int_array.length ; j++)
                  str_array[i] = htref.get(n ew Integer(int_array[j]).toString());

Hope it helps
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
MytixAuthor Commented:
Hi guys, thanks...but that was half of what I was asking...i also half did the unsorting..but what i really wanted as a result was "aeeikprtvy" instead of numbers. how can that be done? ive tried switching the arrays but it doesnt work. thanks again for your help!
0
 
suprapto45Commented:
Hi,

Try this.

/*
 * Created on Apr 15, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.experts;

import java.util.Arrays;

/**
 * @author user
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Sort
{
      static private String test = "privatekey";
      
      public static void main(String[] args)
      {
            char[] a = new char[test.length()];
            a = test.toCharArray();
            
            Arrays.sort(a);
            
            System.out.println(a);
                        
      }
}

regards
Dave
0
 
aozarovCommented:
What is exactly your input to the last part you are looking for?
Is it:
 [6,7,4,9,1,8,2,5,3,10] and "privatekey"
and the output should be
 "aeeikprtvy" ?

If not then can you please state what is the given input and what is the desired output?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.