Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Apr 28, 2000

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 the
novelty 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?
Jeremy Chen
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date