SORTING ARRAYS
This tip discusses how you can sort data in arrays. Sorting arrays
of primitive types is easy. There are seven methods in the class
Arrays for sorting arrays of each of the seven primitive types:
byte, char, double, float, int, long, and short. Here's an example
that sorts an array of doubles.
import java.util.*;
import java.awt.*;
class Sort1 {
// Sorts an array of random 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();
}
// Sort the array.
Arrays.sort(dblarr);
file://Print the array
for (int i=0; i<dblarr.length; i++){
System.out.println(dblarr[i]);
}
}
}
Sorting an array of objects is just as easy if the objects implement
the Comparable interface, java.util.Comparable. This interface gives
a natural ordering for a class so that objects of that class can be
sorted. Here's an example that sorts an array of type String that
implements Comparable.
import java.util.*;
import java.awt.*;
class Sort2 {
// Sorts the arguments in args.
public static void main(String[] args) {
Arrays.sort(args);
file://Print the arguments in args
for (int i=0; i<args.length; i++){
System.out.println(args[i]);
}
}
}
What if the objects do not implement Comparable? Well, you've got
two choices: you can modify the objects to implement Comparable, or
you can supply a Comparator to the sort method. Let's look at the
first option first.
To make an object comparable you need to add Comparable to the
object's implements list. You then need to modify the object to
implement the compareTo() method. The compareTo() method compares
the object with another object of the same type. If the object should
appear before the other object, compareTo() should return a negative
number. If the object should appear after the other object,
compareTo() should return a non-zero positive number. Zero should be
returned if the objects are equal.
Point is an AWT class that is not comparable. The following example
creates a version of Point that is comparable. It sorts points by
distance from the origin.
import java.util.*;
import java.awt.*;
class MyPoint extends java.awt.Point implements Comparable {
MyPoint(int x, int y) {
super(x, y);
}
public int compareTo(Object o) {
MyPoint p = (MyPoint)o;
double d1 = Math.sqrt(x*x + y*y);
double d2 = Math.sqrt(p.x*p.x + p.y*p.y);
if (d1 < d2) {
return -1;
} else if (d2 < d1) {
return 1;
}
return 0;
}
}
class Sort3 {
public static void main(String[] args) {
Random rnd = new Random();
MyPoint[] points = new MyPoint[10];
for (int i=0; i<points.length; i++) {
points[i] = new MyPoint(rnd.nextInt(100), rnd.nextInt(100));
}
Arrays.sort(points);
file://Print the points
for (int i=0; i<points.length; i++){
System.out.println(points[i]);
}
}
}
If you can't or don't want to make an object Comparable, you can
supply a Comparator object to the Arrays.sort() method. The
Comparator object must implement two methods: compare() and
equals(). The behaviour of the compare() method is almost identical
to the compareTo() method of the Comparable interface. The equals()
method simply returns true if the two objects passed to it are equal.
The next example is similar to the one above. However, instead of
creating a special kind of Point, we create a comparator that can
sort Point objects.
import java.util.*;
import java.awt.*;
class PointComparator implements Comparator {
public int compare(Object o1, Object o2) {
Point p1 = (Point)o1;
Point p2 = (Point)o2;
double d1 = Math.sqrt(p1.x*p1.x + p1.y*p1.y);
double d2 = Math.sqrt(p2.x*p2.x + p2.y*p2.y);
if (d1 < d2) {
return -1;
} else if (d2 < d1) {
return 1;
}
return 0;
}
public boolean equals(Object o1, Object o2) {
return compare(o1, o2) == 0;
}
}
class Sort4 {
public static void main(String[] args) {
Random rnd = new Random();
Point[] points = new Point[10];
for (int i=0; i<points.length; i++) {
points[i] = new Point(rnd.nextInt(100), rnd.nextInt(100));
}
Arrays.sort(points, new PointComparator());
file://Print the points
for (int i=0; i<points.length; i++){
System.out.println(points[i]);
}
}
}
|