Lijst een reeks snaren in alfabetische volgorde

Ik heb een programma dat de gebruiker een lijst met namen heeft ingevoerd. Ik heb een switch-case naar een functie die ik de namen wil laten afdrukken in alfabetische volgorde.

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

Ik heb beide

geprobeerd

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

en

java.util.Collections.sort(hotel);

Antwoord 1, Autoriteit 100%

raar, uw code lijkt voor mij te werken:

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

Ik liep die code met behulp van “Java Test Bobby Joe Angel” en hier is de output:

$ java Test Bobby Joe Angel
Angel
Bobby
Joe

Antwoord 2, Autoriteit 17%

Het eerste dat u probeerde lijkt te werken. Hier is een voorbeeldprogramma.
Druk op de knop “Start” bovenaan deze pagina om het uit te voeren om de uitvoer zelf te bekijken.

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

Antwoord 3, autoriteit 10%

Je kunt gewoon Arrays#sort()gebruiken, het werkt perfect.
Zie dit voorbeeld:

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

Antwoord 4, autoriteit 7%

java.util.Collections.sort(listOfCountryNames, Collator.getInstance());

Antwoord 5, autoriteit 3%

Hier is een code die werkt:

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

Antwoord 6, autoriteit 3%

In alfabetische volgorde neem ik aan dat de volgorde is: A|a < B|b < C|c…
Ik hoop dat dit is waar @Nick naar op zoek is (of was) en dat het antwoord de bovenstaande veronderstelling volgt.

Ik zou willen voorstellen om een klasse-implementatiemethode van de Comparator-interface te implementeren als:

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

en roep vanuit de aanroepmethode de methode Arrays.sort aan met aangepaste Comparator als:

Arrays.sort(inputArray, customComparator);

Geobserveerde resultaten:
input Array: “Vani”, “Kali”, “Mohan”, “Soni”, “kuldeep”, “Arun”

uitvoer (alfabetische volgorde) is:
Arun, Kali, kuldeep, Mohan, Soni, Vani

Output(Natural-order door Arrays.sort(inputArray) uit te voeren is:
Arun, Kali, Mohan, Soni, Vani, kuldeep

Dus in het geval van natuurlijke ordening, [Vani < kuldeep] wat naar mijn begrip van alfabetische volgorde niet het gewenste is.

ga voor meer begrip van natuurlijke en alfabetische/lexicale volgorde naar discussie hier


Antwoord 7, autoriteit 3%

Arrays.sort(stringArray);
Hiermee wordt de tekenreeksarray gesorteerd op basis van de Unicode-tekenwaarden. Alle tekenreeksen die aan het begin van de tekenreeks hoofdletters bevatten, staan alfabetisch bovenaan de gesorteerde lijst, gevolgd door alle tekenreeksen met kleine letters.
Dus als de array strings bevat die beginnen met zowel hoofdletters als kleine letters, zou de gesorteerde array geen hoofdletterongevoelige alfabetische lijst retourneren

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

Uitvoer is: Alice, Carol, bob,

Als je wilt dat de Strings gesorteerd worden zonder hoofdletters, heb je een tweede argument nodig, een Comparator, voor Arrays.sort(). Zo’n comparator is al voor ons geschreven en is toegankelijk als een static in de String-klasse met de naam CASE_INSENSITIVE_ORDER.

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

Uitvoer is: Alice, bob, Carol


Antwoord 8, autoriteit 3%

U kunt de methode Arrays.sort() gebruiken. Hier is het voorbeeld,

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

Uitvoer

[brooke, cameron, frederick, peter, taylor]


Antwoord 9, autoriteit 3%

CompareTo()-methode: de twee tekenreeksen worden vergeleken op basis van Unicode-tekenwaarden.

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

Antwoord 10

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

Antwoord 11

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

Other episodes