Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Multidimensional arrays 1

Status
Not open for further replies.

plotzer

Programmer
Aug 28, 2001
35
0
0
US
I've been attempting to find a way to sort a multidensional array. It is a two dimensional array containing distances and city names. For example:

String idArray[][];
idArray = new String[3][2];


with the following values:
10,name1
30,name2
4,name3

I want to sort the entire array based on the array column n,0 containing the distances. I tried the Arrays.sort method but it appears to work only on arrays with one dimension.

Anybody have any ideas. I've been looking at the collections framework and ArrayLists but I havent really seen an easy answer. I am currently saving each value to it's own array and doing a bubble sort on both arrays simultaneously. Hoping their is a better way.

Thanks
 
I don't know of any "ready-made method", but the below algorithm will do it quite neatly.

(mainly nicked from : )
Code:
		String array[][] = new String[3][2];
		array[0][0] = "40";
		array[1][0] = "30";
		array[2][0] = "10";
		array[0][1] = "name1";
		array[1][1] = "name2";
		array[2][1] = "name3";

		System.err.println("\nBefore : ");
		for (int i = 0; i < array.length; i++) {
			System.err.println(array[i][0] +" " +array[i][1]);
		}

		int n = array.length;
		boolean doMore = true;
		while (doMore) {
			n--;
			doMore = false;  // assume this is our last pass over the array
			for (int i = 0; i < n; i++) {
				int x1 = Integer.parseInt(array[i][0]);
				int x2 = Integer.parseInt(array[i+1][0]);
				if (x1 > x2) {
					// exchange elements
					String temp = array[i][0];
					array[i][0] = array[i+1][0];
					array[i+1][0] = temp;
					temp = array[i][1];
					array[i][1] = array[i+1][1];
					array[i+1][1] = temp;
					doMore = true;  // after an exchange, must look again
				}
			}
		}

		System.err.println("\nAfter : ");
		for (int i = 0; i < array.length; i++) {
			System.err.println(array[i][0] +" " +array[i][1]);
		}

Interesting sorting algorithms :

 
Here you go:
Code:
import java.util.*;
public class Test {

    public static class City implements Comparable {
        private String name;
        private int distance;
        public City(String name, int distance) {
            this.name = name;
            this.distance = distance;
        }
        public int compareTo(Object o) {
            City c = (City) o;
            return this.distance - c.distance;
        }
        public String toString() {
            return "["+name+", "+distance+"]";
        }
    }

    public static void main(String[] args) {
        List cities = new ArrayList();
        cities.add(new City("name1",40));
        cities.add(new City("name2",30));
        cities.add(new City("name3",10));
        Collections.sort(cities);
        Iterator it  = cities.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
Cheers, Neil
 
Toolkit,

Thanks. This works nicely
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top