How to sort by an arrayList instead of arrays

I have a assignment that requires me to add a few features to a previous project. I must add a selection sort to a file with data on each line in this style ( california,30856621 ) and I must use an arraylist, since my previous assignment used this.

I must use a selection sort to sort by name and display name only, then I must use a selection sort to sort by the population with the name displayed after that. Here is some code for another assignment that is a selection sort but it uses arrays instead of arraylist and it sorts just numbers.

Can anybody help me change this code so it will sort by the name from an arraylist. I have working code that grabs the name from the data which I called getName

the code I based my new selection sort of arrayList on :

static void selectionSort(int[] A) {

// Sort A into increasing order, using selection sort

for (int lastPlace = A.length-1; lastPlace > 0; lastPlace--) {

int maxLoc = 0; // Location of largest item seen so far.

for (int j = 1; j <= lastPlace; j++) {

if (A[j] > A[maxLoc]) {

maxLoc = j;

}

}

int temp = A[maxLoc]; // Swap largest item with A[lastPlace].

A[maxLoc] = A[lastPlace];

A[lastPlace] = temp;

} // end of for loop

}

I have tried and tried and can't get it to work. I know some of the style changes like i must use compareTo instead of > style signs. I am having trouble getting my swapping of the arrayList to work which seems easy but I just can't achieve it or get it to compile.

-Thanks ahead of time

[1612 byte] By [RL206a] at [2007-10-2 6:45:47]
# 1
swapping is easy: call Collections.swap(list, index1, index2).you can also do it with two calls to get(index) and set(index), but why rewrite the wheel.
jsalonena at 2007-7-16 13:54:23 > top of Java-index,Java Essentials,Java Programming...
# 2

Hope below code will give you proper idea!!!!!!!!

import java.util.ArrayList;

public class Solution {

/* Sorting type. */

private static final int CITY = 1;

private static final int POPULATIION = 2;

class FileData {

String city;

long population;

public FileData(String city, long population) {

this.city = city;

this.population = population;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public long getPopulation() {

return population;

}

public void setPopulation(long population) {

this.population = population;

}

}

public static void main(String[] args) {

ArrayList arrayList = new ArrayList();

/*

* Code that reads file and populates arraylist. e.g.

* arrayList.add(fileData);

*

*/

Solution solution = new Solution();

arrayList.add(0, solution.new FileData("A", 5000000));

arrayList.add(0, solution.new FileData("C", 100000));

arrayList.add(0, solution.new FileData("D", 500000));

arrayList.add(0, solution.new FileData("B", 600000));

arrayList.add(0, solution.new FileData("E", 4000000));

selectionSort(arrayList,POPULATIION);

printValues(arrayList);

}

static void selectionSort(ArrayList list, int sortBy) {

final int size = list.size();

for (int outer = 0; outer < size - 1; outer++) {

for (int inner = outer + 1; inner < size; inner++) {

if (sortBy == CITY) {

if (((FileData) list.get(outer)).getCity().compareTo(

((FileData) list.get(inner)).getCity()) > 0) {

swap(list, inner, outer);

}

} else {

if (((FileData) list.get(inner)).getPopulation() > ((FileData) list

.get(outer)).getPopulation()) {

swap(list, inner, outer);

}

}

}

}

}

static void swap(ArrayList list, int firstPos, int secondPos) {

Object temp = list.get(firstPos);

list.set(firstPos, list.get(secondPos));

list.set(secondPos, temp);

}

static void printValues(ArrayList list) {

final int size = list.size();

for (int counter = 0; counter < size; counter++) {

FileData fileData = (FileData) list.get(counter);

System.out.println(fileData.getCity() + " "

+ fileData.getPopulation());

}

}

}

mk2004a at 2007-7-16 13:54:23 > top of Java-index,Java Essentials,Java Programming...