Sorted the alphabetical names according to their ASCII value in Java Swing

In the post, we will learn how to sort the name according to ASCII value. In this program, we have created the 3 Input dialog box and input three different names and after this, the name will be set according to their ASCII value in ascending order. These steps are mandatory in order to sort strings by ASCII value in Java Swing.

  1. Import Java Swing Class.
  2. Create Three Dialog Box

Sort Strings By ASCII Value in Java Swing

First, we import the java swing class after this we take a 3 variable of string type for storing the values from the input dialog box after this we compare the input according to ASCII value. The ASCII value of A to Z and a to z is available on Google.

import javax.swing.*;
public class SortedNames
{
  public static void main(String s[])
  {
    String name1;
  String name2;
  String name3;
  String output = "";
  
  name1=JOptionPane.showInputDialog("please enter name 1");
  name2=JOptionPane.showInputDialog("please enter name 2");
  name3=JOptionPane.showInputDialog("please enter name 3");
  
  if(name1.compareToIgnoreCase(name2)<0&&name1.compareToIgnoreCase(name3)<0)
  {
    output+=name1;
    if(name2.compareToIgnoreCase(name3)<0)
    {
      output+=name2;
      output+=name3;
    }
    else
    {
      output+=name3;
      output+=name2;
    }

  }
  else if(name2.compareToIgnoreCase(name1)<0&&name2.compareToIgnoreCase(name3)<0)
  {
    output+=name2;
    if(name1.compareToIgnoreCase(name3)<0)
    {
      output+=name1;
      output+=name3;
    }
    else
    {
      output+=name3;
      output+=name1;
    }

  }
  else if(name3.compareToIgnoreCase(name2)<0&&name3.compareToIgnoreCase(name2)<0)
  {
    output+=name3;
    if(name2.compareToIgnoreCase(name3)<0)
    {
      output+=name2;
      output+=name1;
    }
    else
    {
      output+=name1;
      output+=name2;
      
    }
  }
}

  }

In this code, we can see the logic we have used.

You may also learn,

The output of the program:



 

Output of the first input dialog box

Output of the first input dialog box

 

Output of the second input dialog box

Output of the second input dialog box


Output of the third input dialog box

Output of the third input dialog box

Final output : Sorted strings by ascii value

sort strings by ascii value in Java Swing

final output of the program

 

First we give the name in the input dialog box is Mayank, second we give the name is Sahil and third, we give the name is Kaushal.

Leave a Reply

Your email address will not be published. Required fields are marked *