How to resolve the algorithm Sort three variables step by step in the Java programming language
How to resolve the algorithm Sort three variables step by step in the Java programming language
Table of Contents
Problem Statement
Sort (the values of) three variables (X, Y, and Z) that contain any value (numbers and/or literals). If that isn't possible in your language, then just sort numbers (and note if they can be floating point, integer, or other). I.E.: (for the three variables x, y, and z), where: After sorting, the three variables would hold: For numeric value sorting, use: I.E.: (for the three variables x, y, and z), where: After sorting, the three variables would hold: The variables should contain some form of a number, but specify if the algorithm used can be for floating point or integers. Note any limitations. The values may or may not be unique. The method used for sorting can be any algorithm; the goal is to use the most idiomatic in the computer programming language used. More than one algorithm could be shown if one isn't clearly the better choice.
One algorithm could be:
Another algorithm (only for numeric values):
Show the results of the sort here on this page using at least the values of those shown above.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sort three variables step by step in the Java programming language
1. Introduction
This Java code demonstrates how to sort three variables of different types (primitive integers, strings, and custom objects) using the Java Stream API and Comparator
interface.
2. Sorting Primitive Integers
- The
int
type (iA
,iB
, andiC
) variables are sorted using theStream.sorted()
method. - The sorted integers are stored in an
int[]
array calledsortedInt
. - The original
int
variables are updated with the sorted values using array indices. - The sorted values are printed using
System.out.printf
.
3. Sorting Strings
- The
String
type (sA
,sB
, andsC
) variables are sorted using theStream.sorted()
method. - The sorted strings are stored in an
Object[]
array calledsortedStr
to accommodate the generic nature ofStream
. - The sorted strings are converted back to individual
String
variables using casting. - The sorted values are printed using
System.out.printf
.
4. Sorting Custom Objects
- Three
Box
objects (bA
,bB
, andbC
) represent custom objects with aweightKg
field. - The
Comparator.comparingInt
method is used to provide a comparator that compares theweightKg
field ofBox
objects. - The sorted
Box
objects are stored in anObject[]
array calledsortedBox
. - The sorted
Box
objects are converted back to individualBox
variables using casting. - The sorted values are printed using
System.out.printf
.
5. Output
The program prints the sorted values for integers, strings, and custom objects, demonstrating how the Stream.sorted()
method and custom comparators can be used to sort data of different types in Java.
Source code in the java programming language
import java.util.Comparator;
import java.util.stream.Stream;
class Box {
public int weightKg;
Box(final int weightKg) {
this.weightKg = weightKg;
}
}
public class Sort3Vars {
public static void main(String... args) {
int iA = 21;
int iB = 11;
int iC = 82;
int[] sortedInt = Stream.of(iA, iB, iC).sorted().mapToInt(Integer::intValue).toArray();
iA = sortedInt[0];
iB = sortedInt[1];
iC = sortedInt[2];
System.out.printf("Sorted values: %d %d %d%n", iA, iB, iC);
String sA = "s21";
String sB = "s11";
String sC = "s82";
Object[] sortedStr = Stream.of(sA, sB, sC).sorted().toArray();
sA = (String) sortedStr[0];
sB = (String) sortedStr[1];
sC = (String) sortedStr[2];
System.out.printf("Sorted values: %s %s %s%n", sA, sB, sC);
Box bA = new Box(200);
Box bB = new Box(12);
Box bC = new Box(143);
// Provides a comparator for Box instances
Object[] sortedBox = Stream.of(bA, bB, bC).sorted(Comparator.comparingInt(a -> a.weightKg)).toArray();
bA = (Box) sortedBox[0];
bB = (Box) sortedBox[1];
bC = (Box) sortedBox[2];
System.out.printf("Sorted Boxes: %dKg %dKg %dKg%n", bA.weightKg, bB.weightKg, bC.weightKg);
}
}
You may also check:How to resolve the algorithm Digital root step by step in the Elixir programming language
You may also check:How to resolve the algorithm Repeat step by step in the Java programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the XSLT 2.0 programming language
You may also check:How to resolve the algorithm Biorhythms step by step in the Fortran programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the F# programming language