• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to Sort Array Of String According To String Length

WAP to Sort Array Of String According To String Length

Last Updated on: July 25, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Sort Array Of String According To Strig Length

WAP to Sort Array Of String According To String Length: The program InterviewPrograms52 aims to sort an array of strings based on the lengths of the strings in ascending order.

In this program, we have used the concepts of For Loop and If Statement. For better understanding, you can follow the tutorials which we have already shared by clicking on the below link:

For Loop In Java
If Statement In Java

Sort Array Of String According To Strig Length

package com.softwaretestingo.interviewprograms;
import java.util.Arrays;
public class InterviewPrograms52 
{
	/*
	 * Input { "Hi" , "maven" , "selenium " , "java" } 
	 * Output{ "Hi", "java", "maven" , "selenium" }
	 */
	public static void main(String[] args) 
	{
		String ip[]={"Hi","maven","selenium","java"};

		for(int i=0;i<ip.length;i++)
		{
			for(int j=i+1;j<ip.length;j++)
			{
				if(ip[i].length()>ip[j].length())
				{
					String temp=ip[j];
					ip[j]=ip[i];
					ip[i]=temp;
				}
			}
		}
		System.out.println(Arrays.toString(ip));
	}
}

Output

[Hi, java, maven, selenium]

Here’s how the program works:

  1. The input array ip contains four strings: “Hi”, “maven”, “selenium”, and “java”.
  2. The program uses nested loops to compare each string’s length with all other strings in the array.
  3. Starting from the first string (“Hi”), it compares its length with the lengths of the other strings (“maven”, “selenium”, and “java”).
  4. If the length of the current string (“Hi”) is greater than the length of any other string, it swaps their positions in the array.
  5. The program continues this process until the array is fully sorted based on the lengths of the strings in ascending order.
  6. After sorting, the program prints the sorted array to the console using Arrays.toString(ip).

In this specific case, the program will output:

[Hi, java, maven, selenium]

The output represents the array of strings sorted based on their lengths in ascending order. The string “Hi” has the shortest length, followed by “java”, “maven”, and “selenium,” which has the longest length.

    WAP to Print an Increasing Number of Repetitions of the Input Number's Characters
    WAP to Increasing Number of Repetitions of Input Number’s Characters
    Reverse Number In Java
    Reverse Number In Java
    Kth Largest Element in an Array
    WAP to Find Kth Largest Element in an Array
    Odd and Even Numbers In Java
    How to Find Odd and Even Numbers In Java?
    WAP to Print Character and Occurrences Count
    WAP to Print Character and Occurrences Count
    Search a Letter In a String
    WAP For Search a Letter In a String
    WAP to Split An Array Into Two Equal Sum Subarrays
    WAP to Split An Array Into Two Equal Sum Subarrays
    WAP to Check Character Sequence Is in Same Order Or Not (isomorphic)
    WAP to Check Character Sequence Is in Same Order Or...
    WAP to Repeat Each Letter According to its Corresponding Count
    WAP to Print Character by its Corresponding Count
    ReflectionAPI Example
    Write a Program to Find out ReflectionAPI Example Program?

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers