List of string arrays in alphabetical order

I have a program in which the user enters a list of names. I have a case of switching to a function that I would like the names to be printed in alphabetical order.

public static void orderedGuests(String[] hotel) { //?? } 

I tried both

 Arrays.sort(hotel); System.out.println(Arrays.toString(hotel)); 

and

 java.util.Collections.sort(hotel); 
+19
source share
11 answers

Strange, your code works for me:

 import java.util.Arrays; public class Test { public static void main(String[] args) { // args is the list of guests Arrays.sort(args); for(int i = 0; i < args.length; i++) System.out.println(args[i]); } } 

I ran this code using the "java Test Bobby Joe Angel", and here is the result:

 $ java Test Bobby Joe Angel Angel Bobby Joe 
+22
source

The first thing you tried seems to work fine. Here is an example program.
Click the "Start" button at the top of this page to launch it to see the result 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)); } } 
+4
source

You can just use Arrays#sort() , it works fine. 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]); } 
+2
source

Here is the 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)); } } 
+1
source
 java.util.Collections.sort(listOfCountryNames, Collator.getInstance()); 
+1
source

In alphabetical order, I assume that the order should be: A | a <B | b <C | C ... I hope this is what @Nick (or was) looking for, and the answer follows the above assumption.

I would suggest using the class comparison method for the Comparator interface class:

 public int compare(Object o1, Object o2) { return o1.toString().compareToIgnoreCase(o2.toString()); } 

and from the calling method, the Arrays.sort method with the custom Comparator is called as:

 Arrays.sort(inputArray, customComparator); 

Observed results: input array: “Vani”, “Kali”, “Mohan”, “Sony”, “Kuldep”, “Arun”

conclusion (in alphabetical order): Arun, Kali, cooldep, Mohan, Sony, Vani

Conclusion (Natural order by executing Array.sort (inputArray) array: Arun, Kali, Mohan, Sony, Vani, Kuldep

Thus, in the case of natural ordering, [Vani <kuldeep], which, to my understanding of the alphabetical order, is not desirable.

for a better understanding of the natural and alphabetic / lexical order, visit the discussion here

+1
source

You can use the Arrays.sort () method. Here is an 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)); } } 

Exit

[Brooke, Cameron, Frederick, Peter, Taylor]

+1
source

CompareTo() : Two lines are compared based on Unicode character values.

 import java.util.*; public class Test { int n,i,temp; String names[n]; public static void main(String[] args) { String names[5] = {"Brian","Joshua","Louis","David","Marcus"}; for(i=0;i<5;i++){ for(j=i+1;i<n;j++){ if(names[i].CompareTo(names[j]>0) { temp=names[i]; names[i]=names[j]; names[j]=temp; } else System.out.println("Alphabetically Ordered"); } } } 
+1
source
 **//With the help of this code u not just sort the arrays in alphabetical order but also can take string from user or console or keyboard import java.util.Scanner; import java.util.Arrays; public class ReadName { final static int ARRAY_ELEMENTS = 3; public static void main(String[] args) { String[] theNames = new String[5]; Scanner keyboard = new Scanner(System.in); System.out.println("Enter the names: "); for (int i=0;i<theNames.length ;i++ ) { theNames[i] = keyboard.nextLine(); } System.out.println("**********************"); Arrays.sort(theNames); for (int i=0;i<theNames.length ;i++ ) { System.out.println("Name are " + theNames[i]); } } }** 
0
source

Arrays.sort (StringArray); This sorts the string array based on Unicode character values. All lines containing the upper characters at the beginning of the line will be located at the top of the sorted list in alphabetical order, and then all lines with lowercase characters. Therefore, if the array contains strings starting with both uppercase and lowercase characters, the sorted array does not return a case-insensitive alphabetical list,

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

Conclusion: Alice, Carol, Bob,

If you want to sort the strings out of the way, you'll need the second Comparator argument for Array.sort (). Such a comparator has already been written for us and can be accessed as a static object in the String class with the name CASE_INSENSITIVE_ORDER.

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

Conclusion: Alice, Bob, Carol

0
source
  public static String[] textSort(String[] words) { for (int i = 0; i < words.length; i++) { for (int j = i + 1; j < words.length; j++) { if (words[i].compareTo(words[j]) > 0) { String temp = words[i]; words[i] = words[j]; words[j] = temp; } } } return words; } 
0
source

All Articles