Interview Question Sort an array of Strings.


You can sort an array of strings according to their length using Arrays.sort(), Comparator, sorting algorithm.

Atos Interview Question Sort an array of Strings using length
 Sort array of Strings using length
There are a number of better ways to do it. This is a length-based sort, there are a number of Array of int that corresponds to the lengths of the words at the same respective index. Then you can run your logic on the int value and be sure to Swap the strings around at the same time. The end result would be a Sort array of Strings.
approaches that can be taken. You are going to have to calculate the length of each String at some point. You can do is create an

If you want to print the shortest element first in a given String of Arrays or Sort an array of Strings based on length.

Then this below program or code is for sorting Strings based on length.

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortStringUsingComparator {

 public static void main(String[] args) {

  String input = "We are Learning java online";
  List<String> list = Arrays.asList(input.split(" "));
  System.out.println(list);
  Comparator<String> comp = new Comparator<String>() {
   @Override
   public int compare(String o1, String o2) {
    return o1.length() - o2.length();
   }
  };
  Collections.sort(list, comp);
  System.out.println(list);
 }
}

Before Sorting 
OUTPUT:[We, are, Learning, java, online] 

 After Sorting 
OUTPUT:[We, are, java, online, Learing]

Sorting Strings based on length using Lambda Expression.


import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortStringUsingComparator {

 public static void main(String[] args) {

  String input = "We are Learning java online";
  List<String> list = Arrays.asList(input.split(" "));
  System.out.println(list);

  // Using Lambda Expression
  Comparator<String> comp = (String o1, String o2) -> {
   return o1.length() - o2.length();
  };

  Collections.sort(list, comp);
  System.out.println("First:" + list);

  // Second way
  Collections.sort(list, (o1, o2) -> {
   return o1.length() - o2.length();
  });

  System.out.println("Second:" + list);

  // Third way
  Collections.sort(list, (o1, o2) -> o1.length() - o2.length());
  System.out.println("Third:" + list);

 }
}

Before Sorting 
OUTPUT:[We, are, Learning, java, online] 

 After Sorting 
First:[We, are, java, online, Learing]
Second:[We, are, java, online, Learning]
Third:[We, are, java, online, Learning]

Sorting String based on length using Sorting Algorithm.



import java.util.Arrays;

public class SortStringAlgorithm {

 public static void main(String[] args) {

  String input = "We are Learning java online";
  String arr[] = input.split(" ");
  System.out.println(Arrays.toString(arr));

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

Before Sorting 
OUTPUT:[We, are, Learning, java, online] 

 After Sorting 
OUTPUT:[We, are, java, online, Learing]

Post a Comment

Previous Post Next Post