List an Array of Strings in alphabetical order

前端 未结 11 1387
独厮守ぢ
独厮守ぢ 2020-12-28 14:49

I have a program which has the user inputs a list of names. I have a switch case going to a function which I would like to have the names print off in alphabetical order.

相关标签:
11条回答
  • 2020-12-28 14:49

    Here is code that works:

    import java.util.Arrays;
    import java.util.Collections;
    
    public class Test
    {
        public static void main(String[] args)
        {
            orderedGuests1(new String[] { "c", "a", "b" });
            orderedGuests2(new String[] { "c", "a", "b" });
        }
    
        public static void orderedGuests1(String[] hotel)
        {
            Arrays.sort(hotel);
            System.out.println(Arrays.toString(hotel));
        }
    
        public static void orderedGuests2(String[] hotel)
        {
            Collections.sort(Arrays.asList(hotel));
            System.out.println(Arrays.toString(hotel));
        }
    
    }
    
    0 讨论(0)
  • 2020-12-28 14:51
    java.util.Collections.sort(listOfCountryNames, Collator.getInstance());
    
    0 讨论(0)
  • 2020-12-28 14:53

    You can just use Arrays#sort(), it's working perfectly. See this example :

    String [] a = {"English","German","Italian","Korean","Blablablabla.."};
    //before sort
    for(int i = 0;i<a.length;i++)
    {
      System.out.println(a[i]);
    }
    Arrays.sort(a);
    System.out.println("After sort :");
    for(int i = 0;i<a.length;i++)
    {
      System.out.println(a[i]);
    }
    
    0 讨论(0)
  • 2020-12-28 14:56

    You can use Arrays.sort() method. Here's the example,

    import java.util.Arrays;
    
    public class Test 
    {
        public static void main(String[] args) 
        {
            String arrString[] = { "peter", "taylor", "brooke", "frederick", "cameron" };
            orderedGuests(arrString);
        }
    
        public static void orderedGuests(String[] hotel)
        {
            Arrays.sort(hotel);
            System.out.println(Arrays.toString(hotel));
        }
    }
    

    Output

    [brooke, cameron, frederick, peter, taylor]

    0 讨论(0)
  • 2020-12-28 15:02

    Arrays.sort(stringArray); This sorts the string array based on the Unicode characters values. All strings that contain uppercase characters at the beginning of the string will be at the top of the sorted list alphabetically followed by all strings with lowercase characters. Hence if the array contains strings beginning with both uppercase characters and lowercase characters, the sorted array would not return a case insensitive order alphabetical list

    String[] strArray = { "Carol", "bob", "Alice" };
    Arrays.sort(strList);
    System.out.println(Arrays.toString(hotel));
    

    Output is : Alice, Carol, bob,

    If you require the Strings to be sorted without regards to case, you'll need a second argument, a Comparator, for Arrays.sort(). Such a comparator has already been written for us and can be accessed as a static on the String class named CASE_INSENSITIVE_ORDER.

    String[] strArray = { "Carol", "bob", "Alice" };
    Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER);
    System.out.println(Arrays.toString(strArray ));
    

    Output is : Alice, bob, Carol

    0 讨论(0)
  • 2020-12-28 15:03

    The first thing you tried seems to work fine. Here is an example program.
    Press the "Start" button at the top of this page to run it to see the output yourself.

    import java.util.Arrays;
    
    public class Foo{
        public static void main(String[] args) {
            String [] stringArray = {"ab", "aB", "c", "0", "2", "1Ad", "a10"};
            orderedGuests(stringArray);
        }
    
        public static void orderedGuests(String[] hotel)
        {
            Arrays.sort(hotel);
            System.out.println(Arrays.toString(hotel));
        }
    }
    
    0 讨论(0)
提交回复
热议问题