java sort String alphabetically code example

Example 1: js order alphabetically

// Alphabetically
const ascending = data.sort((a, b) => a[field].localeCompare(b[field]))
// Descending
const descending = ascending.reverse()

Example 2: java sort string characters alphabetically

// java sort string characters alphabetically
import java.util.Arrays;
public class CharactersAlphabetically
{
   public static void main(String[] args)
   {
      String strInput = "flowerbrackets";
      // converting string to char array
      char[] ch = strInput.toCharArray();
      // sorting char array
      Arrays.sort(ch);
      // converting char array to string
      String strSorted = String.valueOf(ch);
      System.out.println("sort string characters alphabetically: " + strSorted);
   }
}

Example 3: Arrange words of a sentence in alphabetical order in java

// Arrange words of a sentence in alphabetical order in java
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class ArrangeInAlphabeticalOrder 
{
   public static void main(String[] args) 
   {
      Set set = new TreeSet();
      String strInput = "hi all welcome to flower brackets blog";
      System.out.println("Before arranging sentence in alphabetical order: " + strInput);
      StringTokenizer strToken = new StringTokenizer(strInput," ");
      while(strToken.hasMoreElements())
      {
         set.add(strToken.nextElement());
      }
      System.out.println("After arranging sentence in alphabetical order: " + set);
   }
}

Example 4: java sort list alphabetically

Assuming that those are Strings, use the convenient static method sort…

 java.util.Collections.sort(listOfCountryNames)

Example 5: string sorting in java

public class ArrayReturn3 {
 
    public static String[] sortNames(String[] userNames) {
        String temp;
        for (int i = 0; i < userNames.length; i++) {
            for (int j = i + 1; j < userNames.length; j++) {
                if (userNames[i].compareTo(userNames[j]) > 0) {
                    temp = userNames[i];
                    userNames[i] = userNames[j];
                    userNames[j] = temp;
                }
            }
        }
        return userNames;
    }
 
    public static void main(String[] args) {
        String[] names = {"Ram", "Mohan", "Sohan", "Rita", "Anita", "Nita", "Shyam", "Mukund"};
        System.out.println("Names before sort");
        for (String n : names) {
            System.out.print(" " + n);
        }
        String[] sortedNames = sortNames(names);
        System.out.println("\nNames after sort (Sent name)");
        for (String n : names) {
            System.out.print(" " + n);
        }
        System.out.println("\nNames after sort (Received name)");
        for (String n : sortedNames) {
            System.out.print(" " + n);
        }
    }
}

Example 6: java compare strings alphabetically

Different 'Collation Strength' values give different sort results: 
[Äbc, äbc, Àbc, àbc, Abc, abc, ABC] - Original Data
[Äbc, äbc, Àbc, àbc, Abc, abc, ABC] Primary
[Abc, abc, ABC, Àbc, àbc, Äbc, äbc] Secondary
[abc, Abc, ABC, àbc, Àbc, äbc, Äbc] Tertiary

Case kicks in only with Tertiary Collation Strength  : 
[cache, CACHE, Cache] - Original Data
[cache, CACHE, Cache] Primary
[cache, CACHE, Cache] Secondary
[cache, Cache, CACHE] Tertiary

Accents kick in with Secondary Collation Strength.
Compare with no accents present: 
Collator sees them as the same : abc, ABC - Primary
Collator sees them as the same : abc, ABC - Secondary
Collator sees them as DIFFERENT: abc, ABC - Tertiary

Compare with accents present: 
Collator sees them as the same : abc, ÀBC - Primary
Collator sees them as DIFFERENT: abc, ÀBC - Secondary
Collator sees them as DIFFERENT: abc, ÀBC - Tertiary