按字母顺序列出字符串数组

问题描述 投票:0回答:11

我有一个程序,让用户输入姓名列表。我有一个开关盒,用于执行一个功能,我希望按字母顺序打印名称。

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

我都试过了

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

java.util.Collections.sort(hotel);
java arrays function case
11个回答
29
投票

奇怪,你的代码似乎对我有用:

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]);
    }
}

我使用“java Test Bobby Joe Angel”运行该代码,这是输出:

$ java Test Bobby Joe Angel
Angel
Bobby
Joe

5
投票

您尝试的第一件事似乎效果很好。这是一个示例程序。
本页顶部的“开始”按钮运行它,亲自查看输出。

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));
    }
}

5
投票

Arrays.sort(stringArray); 这将根据 Unicode 字符值对字符串数组进行排序。所有在字符串开头包含大写字符的字符串将按字母顺序位于排序列表的顶部,后面是所有包含小写字符的字符串。 因此,如果数组包含以大写字符和小写字符开头的字符串,则排序后的数组将不会返回不区分大小写的字母顺序列表

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

输出是:爱丽丝、卡罗尔、鲍勃、

如果您需要不区分大小写地对字符串进行排序,则需要第二个参数,即 Arrays.sort() 的比较器。这样的比较器已经为我们编写好了,并且可以在名为 CASE_INSENSITIVE_ORDER 的 String 类上作为静态进行访问。

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

输出是:爱丽丝、鲍勃、卡罗尔


3
投票

您只需使用

Arrays#sort()
即可,它运行良好。 看这个例子:

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
投票
java.util.Collections.sort(listOfCountryNames, Collator.getInstance());

1
投票

这是有效的代码:

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
投票

按照字母顺序,我假设顺序是:A|a < B|b < C|c... Hope this is what @Nick is(or was) looking for and the answer follows the above assumption.

我建议有一个类实现比较器接口的比较方法:

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

并从调用方法中使用自定义比较器调用 Arrays.sort 方法:

Arrays.sort(inputArray, customComparator);

观察结果: 输入数组:“Vani”,“Kali”,“Mohan”,“Soni”,“kuldeep”,“Arun”

输出(按字母顺序)是: 阿伦、卡利、库迪普、莫汉、索尼、瓦尼

输出(执行 Arrays.sort(inputArray) 的自然顺序是: 阿伦、卡利、莫汉、索尼、瓦尼、库迪普

因此,在自然排序的情况下,[Vani < kuldeep] which to my understanding of alphabetical-order is not the thing desired.

如需更多了解自然顺序和字母/词汇顺序,请访问 此处讨论


1
投票

CompareTo()
方法:根据Unicode字符值比较两个字符串。

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");                               
         }
     }                              
}

0
投票
**//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
投票
 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
投票

打包sample_project;

导入java.util.Arrays;

公共类 SortStringArrayInAlphabeticalOrder {

public static void main(String[] args) {
    
    String[] st = {"anil","shiva","bala","karthik","shravan","chaitanya"};
    
    Arrays.sort(st);
    
    String sortedArray = Arrays.toString(st);
    
    System.out.println(sortedArray);
    
}

}

© www.soinside.com 2019 - 2024. All rights reserved.