hello
I,ve wrote a program but it doesn,t work correctly
please help me
i want to sort some strings based on the number that should be entered at first of each string for example the strings that you enter should be in this form:

12_hello

it means you should enter a number and then _ then a string
for example if you enter

5_allow
4_how
23_hi

the output should be

4_how
5_allow
23_hi

but my program can't do this
i will write my program here

please correct it for me
thanks in advance

Parvin

package sort;

import javax.swing.*;

public  class arraySort
{
 static String[] minStrSoFar = new String[1024];
 static String sortStr[] = new String[1024];
static int j;
 //***
  //this method sorts entered strings based on the number that have been entered before  ***//


  public static void selectSort(int [] enterNum,int number,String part2[])
 {
   // initialize  the variables
   int posMin=0;
   int temp;
   int k;
   String tempStr = new String ();



   for(int i = 0;i<number -1 ; i++)
   {

     posMin = findPosMin(enterNum, i, number, part2);

   if (posMin != i)
     {
       //this part sorts the part of string that is made up of numbers
       temp = enterNum[i];
       enterNum[i] = enterNum[posMin];
       enterNum[posMin] = temp;

    //this part sorts the part of string that is made up of characters
      tempStr = part2[i];
      // part2[i] = minStrSoFar[i];
    //   minStrSoFar[i] = tempStr;
    part2[i] = sortStr[i];
    sortStr[i] = tempStr;

     }//end of if
   JOptionPane.showMessageDialog(null,part2[i]);
   }//end of for

 }//end of selectSort method


 //this method find the position of minimum between entered numbers

 public static int findPosMin(int[] enterNum , int i,int number,String part2[])
 {

  minStrSoFar[i] = part2[i];
   int posMinSoFar = i;

   for(  j =i+1 ;j<number ;j++)
   {
     if(enterNum [j] <enterNum[posMinSoFar] )
     {
       posMinSoFar = j;
       minStrSoFar[i] = part2[j];
     }//end of if

   }//end of for
 sortStr[i]=minStrSoFar[i];
   return posMinSoFar;

 } //end of findPosMin method

}//end of class arraySort



package sort;

//***
  // this program sorts some strings that begins with number( based on their number)****//
import javax.swing.*;
import java.util.StringTokenizer;

public class sortString
{

  public static void main(String[] args)
  {
    //initialize the variables
    int number;
    String stringNumber = JOptionPane.showInputDialog("how many string do you want to enter?");
     number  = Integer.parseInt(stringNumber);
    String string[] = new String[number];
    String part1[] = new String [number];
    String part2[] = new String [number];
    int enterNum []= new int[number];
    int i;
    for( i = 0; i<number; i++)
    {
      string[i] = new String();
      part1 [i] = new String();
      part2[i] = new String();
      string[i] = JOptionPane.showInputDialog("enter your string");

      StringTokenizer st = new StringTokenizer(string[i],"_");

      //seperating 2 parts of the string
      part1[i] = st.nextToken();
      part2[i] =st.nextToken();
      enterNum[i] = Integer.parseInt(part1[i]);

        }//end of for
    arraySort.selectSort(enterNum,number,part2);

  }//end of main method
}//end of sortString class

Edited 3 Years Ago by pyTony: fixed formating

nobody wants to help me?

Don't bump the thread. People will help you when they are ready.

You're on the right track though, you've tokenised the original string splitting it into two parts. Then you are sorting the string according to its integer. I'll have a look at it later.

This article has been dead for over six months. Start a new discussion instead.