Answers for "sort strings java"

2

How to sort a string array in java

// how to sort string array in java without using sort method
import java.util.Arrays;
public class SortStringArray
{
   public static void main(String[] args)
   {
      String[] strPlaces = {"Great Barrier Reef", "Paris", "BoraBora", "Florence","Tokyo", "Cusco"};
      int size = strPlaces.length;
      for(int a = 0; a < size - 1; a++)
      {
         for(int b = a + 1; b < strPlaces.length; b++)
         {
            if(strPlaces[a].compareTo(strPlaces[b]) > 0)
            {
               String temp = strPlaces[a];
               strPlaces[a] = strPlaces[b];
               strPlaces[b] = temp;
            }
         }
      }
      System.out.println(Arrays.toString(strPlaces));
   }
}
Posted by: Guest on December-09-2020
3

Java program to sort names in an alphabetical order

import java.util.Scanner;
public class SortNamesAlphabeticalOrder
{
   public static void main(String[] args)
   {
      int number;
      String str;
      Scanner sc1 = new Scanner(System.in);
      System.out.println("Please enter number of strings: ");
      number = sc1.nextInt();
      String[] names = new String[number];
      Scanner sc2 = new Scanner(System.in);
      System.out.println("Enter all strings: ");
      for(int a = 0; a < number; a++)
      {
         names[a] = sc2.nextLine();
      }
      for(int a = 0; a < number; a++)
      {
         for(int b = a + 1; b < number; b++)
         {
            // java alphabetical sort
            if(names[a].compareTo(names[b]) > 0)
            {
               str = names[a];
               names[a] = names[b];
               names[b] = str;
            }
         }
      }
      System.out.println("After sorting names in an alphabetical order: ");
      for(int a = 0; a < number - 1; a++)
      {
         System.out.println(names[a] + ", ");
      }
      System.out.print(names[number - 1]);
      sc1.close();
      sc2.close();
   }
}
Posted by: Guest on November-02-2020
1

How to sort a string array in java

// java program to sort an array using Arrays.sort() method
import java.util.Arrays;
public class JavaArraySortMethod
{
   public static void main(String[] args)
   {
      String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
      Arrays.sort(strGiven);
      System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
   }
}
Posted by: Guest on December-09-2020
0

sort string java

import java.util.Arrays;

public class Test
{
    public static void main(String[] args)
    {
        String original = "edcba";
        char[] chars = original.toCharArray();
        Arrays.sort(chars);
        String sorted = new String(chars);
        System.out.println(sorted);
    }
}
Posted by: Guest on April-18-2021
1

sort a string array java

String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop","Neo4j"};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
Posted by: Guest on September-29-2020
0

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);
        }
    }
}
Posted by: Guest on November-26-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language