I have to sort an array with state objects and sort it by region number, and population. I have imported this information from a text file so these are strings. I can sort using one of the fields but cant get it to work with both. It always just ends up sorting the last sort called in the method. For instance in my code it just finishes with sorting the region number, which unsorts the population. Is there anyway to sort by population, and then from that sort, sort the region number. Also, i cant use anything from java.util.
public void insertionSort()
{
int in, out;
for (out = 1; out < getElementCount(); out++)
{
State temp = states[out];
in = out;
while (in > 0 && states[in - 1].getPopulation().compareTo(temp.getPopulation()) > 0)
{
states[in] = states[in - 1];
--in;
}
states[in] = temp;
}
for (out = 1; out < getElementCount(); out++)
{
State temp = states[out];
in = out;
while (in > 0 && states[in - 1].getRegionNumber().compareTo(temp.getRegionNumber()) > 0)
{
states[in] = states[in - 1];
--in;
}
states[in] = temp;
}
}
public void Execute()
{
StateCollection sc = new StateCollection();
String filename = "States.Search.txt";
String file = "States.Input.txt";
String[] stateSearch = new String[15];
String[] state = new String[50];
stateSearch = readFile(filename, stateSearch);
state = readFile(file, state);
for (int i = 0; i < state.length; i++)
{
String stateName = state[i].substring(0, 15).trim();
String stateCapital = state[i].substring(15, 30).trim();
String abbr = state[i].substring(30, 32).trim();
String population = state[i].substring(32, 40).trim();
String region = state[i].substring(40, 55).trim();
String regionNumber = state[i].substring(55).trim();
State s = new State(stateName, stateCapital, abbr, population, region, regionNumber);
sc.add(i, s);
}
sc.bubbleSort();
java.util"? It would be the correct approach to either have yourStateclass implementComparable(which is in packagejava.langby the way) or write aComparator(in packagejava.util) for it.