1 2 Previous Next 20 Replies Latest reply: Oct 3, 2007 1:54 AM by 807605 RSS

    Sorting String Arrays (I have searched)

    807605
      I looked around for how you sort arrays of strings and all I found was the easy way. I know its much easier but I want to do it the long way and I imagine my instructor does too. I have sorted int arrays before bit know its not as simple as manipulating the int comparison code for strings like i attempted below haha. Any ideas or is the short cut way the only real way to do it? Thanks.

      String temp;
      for (int k = 1; k < names.length; k++) {
      temp = names[k];
      int i;
      for (i = k - 1; i >= 0 &&{color:#ff0000} names{color}+{color:#ff0000} > temp{color}; i--)+
      +names[i + 1] = names+;
      ++names[i + 1] = temp
      ++
        • 1. Re: Sorting String Arrays (I have searched)
          807605
          Strings have a compareTo method that compares two Strings lexicographically. There is also a compareToIgnoreCase if you need it. Go to the API and read about the method and see how you can use it in your code.
          • 2. Re: Sorting String Arrays (I have searched)
            807605
            the examples in that api confuse the heck outta me. I usually just search for other's examples, study them to see how they work, and then formulate my own. I understand the logic behind what i need to do, its just getting the syntax.

            Edited by: protivakid on Oct 2, 2007 5:04 PM
            • 3. Re: Sorting String Arrays (I have searched)
              796254
              javaalmanac.com is a good place for examples:

              http://www.exampledepot.com/egs/java.util/pkg.html

              %

              PS - I'm floored at how slow the forum has been all day. They must have done some performance detuning today. Slower than ever!
              • 4. Re: Sorting String Arrays (I have searched)
                807605
                String a = "banana";
                String b = "orange";
                String c = "apple";
                System.out.println(a.compareTo(b));
                System.out.println(b.compareTo(a));
                System.out.println(a.compareTo(c));
                System.out.println(a.compareTo(a));
                Run that, look at the result and figure out how you can use the values returned by the compareTo method to determine how to sort your Strings.
                • 5. Re: Sorting String Arrays (I have searched)
                  807605
                  that works well except for names that begin with the same letter because then it compares the 2nd two letters. Maybe I am missing something.
                  • 6. Re: Sorting String Arrays (I have searched)
                    DrClap
                    protivakid wrote:
                    that works well except for names that begin with the same letter because then it compares the 2nd two letters. Maybe I am missing something.
                    I think you are missing the fact that it's doing the right thing.
                    • 7. Re: Sorting String Arrays (I have searched)
                      796440
                      protivakid wrote:
                      that works well except for names that begin with the same letter because then it compares the 2nd two letters. Maybe I am missing something.
                      What kind of string sorting are you doing where it's not supposed to do that?

                      If you have to put the following in alphabetical order, how do you do it?

                      all
                      approach
                      apple
                      almond
                      alert
                      ale
                      appendix
                      • 8. Re: Sorting String Arrays (I have searched)
                        807605
                        ale?

                        I thought we drank it all.
                        • 9. Re: Sorting String Arrays (I have searched)
                          796440
                          flounder wrote:
                          ale?

                          I thought we drank it all.
                          That's why god invented breweries, delivery trucks, and the interstate highway system.
                          • 10. Re: Sorting String Arrays (I have searched)
                            807605
                            But what if there is a wheat/barley/hops crop failure? Or an oil shortage? Or road works? Or a union strike? We might die of thirst
                            • 11. Re: Sorting String Arrays (I have searched)
                              796440
                              flounder wrote:
                              But what if there is a wheat/barley/hops crop failure? Or an oil shortage? Or road works? Or a union strike? We might die of thirst
                              Might as well drink as much now as possible then, just in case!
                              • 12. Re: Sorting String Arrays (I have searched)
                                807605
                                Like we need a reason!
                                • 13. Re: Sorting String Arrays (I have searched)
                                  807605
                                  My comparison formula seems to be messed up :(. I posted my code for those who care to give her a run. Any advice what I need to modify? Is it something dumb like a "+" instead of a "-" or is my syntax completely useless? Thanks. Any advice will help.

                                  import java.io.*;
                                  import java.util.Scanner;
                                  import java.util.*;

                                  public class SortArray {

                                       /**
                                       * @param args
                                       */
                                       public static void main(String[] args) throws IOException {
                                            // TODO Auto-generated method stub

                                            InputStreamReader stream = new InputStreamReader(System.in);
                                            BufferedReader buffer = new BufferedReader(stream);
                                            String input = "";
                                            Scanner in = new Scanner(System.in);

                                            //ask number of ppl and set up array
                                            int TotalNames;
                                            System.out.print("Enter the total number of names: ");
                                            TotalNames = Integer.parseInt(buffer.readLine());
                                            System.out.println();
                                            final int TOTNAMES = TotalNames;
                                            String[] names = new String[TOTNAMES];
                                            int sum;

                                            //input names
                                            for (int i = 0; i < names.length; i++) {
                                                 System.out.print("Enter a name for person #" + (i) + ": ");
                                                 input = in.nextLine();
                                                 names[i] = input;
                                            }

                                            int temp = 0;
                                            int PlaceHolder = 0;
                                            String tempS = "";
                                            for (int k = 0; k < names.length - 1; k++) {

                                                 int i;
                                                 for (i = k + 1; i < names.length; i++) {

                                                      if (names[k].compareTo(names) < temp
                                                                && names[k].compareTo(names[i]) < 0) {

                                                           PlaceHolder = i;
                                                           tempS = names[k + 1];

                                                           names[k + 1] = names[PlaceHolder];
                                                           names[PlaceHolder] = tempS;
                                                      }

                                                 }

                                            }
                                            for (int i = 0; i < names.length; i++) {
                                                 System.out.println(names[i]);
                                            }
                                       }

                                  }
                                  • 14. Re: Sorting String Arrays (I have searched)
                                    807605
                                    Please use code tags to make your code readable and copy your original code not the unformatted code in the previous reply.

                                    &#91;code&#93;
                                    your code
                                    &#91;/code&#93;
                                    1 2 Previous Next