Home > Tech Tips > How to Sort Two Dimensional Data based on Multiple Parameters?

How to Sort Two Dimensional Data based on Multiple Parameters?

This is a follow up to my previous post How to Sort a Two Dimensional String Array using JAVA

Here I am trying to sort an address table by Street Name, Street Number and City using java comparator. First I sort the table by Street Name (column A), then keeping street name as constant sort by Street Number (column B) and finally keeping street name and number as constant sort by City (column C)

If you want to sort my more columns, you can easily add the rules to ORDER_BY_RULES – shown below.

Address Table:

Street Name Street Number City State Zip Code
univ dr 101 Sunshine FL 33324
break rd 102 Sunrise FL 33224
univ dr 103 Cleveland OH 24785
dykes rd 104 San Antonio TX 24785
dykes rd 104 Boston MA 24785
dykes rd 104 Orlando FL 24783
import java.util.Arrays;
import java.util.Comparator;

public class Sort {

static final Comparator<Address> ORDER_BY_STREETNAME = new Comparator<Address>() {
	public int compare(Address a1, Address a2) {
		return a1.streetName.compareTo(a2.streetName);
	}
};
static final Comparator<Address> ORDER_BY_STREETNUMBER = new Comparator<Address>() {
	public int compare(Address a1, Address a2) {
		return a1.streetNumber.compareTo(a2.streetNumber);
	}
};
static final Comparator<Address> ORDER_BY_CITY = new Comparator<Address>() {
	public int compare(Address a1, Address a2) {
		return a1.city.compareTo(a2.city);
	}
};

static final Comparator<Address> ORDER_BY_RULES = new Comparator<Address>() {
	public int compare(Address a1, Address a2) {
		int i = ORDER_BY_STREETNAME.compare(a1,a2);
		if(i == 0){
			i = ORDER_BY_STREETNUMBER.compare(a1,a2);
			if(i == 0){
				i = ORDER_BY_CITY.compare(a1,a2);
			}
		}
		return i;
	}
};

public static void main(String args[]){
	//Array with address to sort
	Address[] addressArray = new Address[]{
		new Address("univ dr","101", "Sunshine", "FL", "33324"),
		new Address("break rd","102", "Sunrise", "FL", "33224"),
		new Address("univ dr","103", "Cleveland", "OH", "24785"),
		new Address("dykes rd","104", "San Antonio", "TX", "24785"),
		new Address("dykes rd","104", "Boston", "MA", "24785"),
		new Address("dykes rd","104", "Orlando", "FL", "24783"),
	};
	Arrays.sort(addressArray, ORDER_BY_RULES);
	//Print the sorted array
	for(int i=0; i<addressArray.length; i++){
		System.out.println(addressArray[i].toString());
	}
	System.out.println();
}
}

//Address Class
class Address {
	String streetName;
	String streetNumber;
	String city;
	String state;
	String zipCode;
	@Override
	public String toString() {
		return "Address [" + streetName + " | " + streetNumber + " | " +
			city + " | " + state + " | " + zipCode + "]";
	}
	public Address(String streetName, String streetNumber,
				String city, String state, String zipCode) {
		super();
		this.streetName = streetName;
		this.streetNumber = streetNumber;
		this.city = city;
		this.state = state;
		this.zipCode = zipCode;
	}
}

Results – sorted by street name, street number and city

Address [break rd | 102 | Sunrise | FL | 33224]
Address [dykes rd | 104 | Boston | MA | 24785]
Address [dykes rd | 104 | Orlando | FL | 24783]
Address [dykes rd | 104 | San Antonio | TX | 24785
Address [univ dr | 101 | Sunshine | FL | 33324]
Address [univ dr | 103 | Cleveland | OH | 24785]
Share

Categories: Tech Tips Tags:
  1. June 13th, 2010 at 12:32 | #1

    Hello George,

    Your solution very useful for two Dimensional array with same length.

    My question is, can we sort two dimensional array based on some row and based on data length. for example we have an array {{“c”}, {“c”}, {“c”, “d”},{“c”, “a”},{“b”}}. how to get the sorted array like this {{“b”},{“c”},{“c”},{“c”,”a”},{“c”, “d”}}.

    Thx a lot George.

  2. November 20th, 2010 at 01:23 | #2

    Great solution
    Loved it

  1. No trackbacks yet.