devxlogo

Sorting Arrays of Primitive Types

Sorting Arrays of Primitive Types

Sorting arrays of primitive types (byte, char, double, float, int, long, and short) is easy.Example:

 import java.util.*;import java.awt.*;class SortDBL						// Sorts an array of randomly generated double values.{	public static void main(String[] args)	{		double[] dblarr = new double[10];		for (int i=0; i < dblarr.length; i++)		{		      dblarr[i] = Math.random();	// Genertate random variables		}		for (int i=0; i < dblarr.length; i++ /* Imagine using i=i+1 for thenovelty but it gets optimized anyway */)		{      		System.out.print(dblarr[i], " ");	// Before		}		System.out.println;		Arrays.sort(dblarr);			// Sort the array		for (int i=0; i < dblarr.length; i++)		{      		System.out.print(dblarr[i], " ");	// After		}		System.out.println;	}}

Simple isn't it?

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist