Results 1 to 2 of 2
  1. #1
    Zerpol is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Arrow Sorting string Array Values

    I am having a problem on how I couldn't sort my array values. My codes don't work the way I wanted them to:


    int n;
    JTextArea outputArea= new JTextArea();
    String class1[]= new String [8];
    JOptionPane.showMessageDialog(null,"Enter 8 Students for Class 1 ");
    n=1;
    for (int i=0; i<class1.length; i++){
    String input=
    JOptionPane.showInputDialog("Student # : "+n);
    class1[i]=input;
    n++;
    }
    String output= "\n\t Class1 Students\n";

    for(int i=0; i<class1.length; i++)
    output+="\t "+class1[i]+"\t"+"\t\n";

    outputArea.setText(output);
    JOptionPane.showMessageDialog(null, outputArea, "Initializing an Array of int Values", JOptionPane.PLAIN_MESSAGE);

    for (int indx=0; indx<class1.length; indx--){
    int high=1;
    for(int i=2; i<= indx; i++){
    if (class1[i]>class1[high]){
    high=2;
    }
    if (high!=indx){
    int temp= class1[high];
    class1[high]=class1[indx];
    class1[indx]=temp;
    }

    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by Zerpol View Post
    for (int indx=0; indx<class1.length; indx--)
    Do you really mean to decrement it? indx-- ?

    If you want to sort String values, you can store them inside ArrayList and sort by using the Collections.sort(List<T> list).

    See if you can go for that approach.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:42 AM
  2. Replies: 2
    Last Post: 10-07-2009, 07:24 PM
  3. Replies: 7
    Last Post: 09-23-2009, 08:28 PM
  4. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 08:59 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •