java - Using an insertion sort algorithm using more than one field from an array of objects -


i have sort array state objects , sort region number, , population. have imported information text file these strings. can sort using 1 of fields cant work both. ends sorting last sort called in method. instance in code finishes sorting region number, unsorts population. there anyway sort population, , sort, sort region number. also, cant use java.util.

public void insertionsort()     {         int in, out;          (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;          }          (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);          (int = 0; < 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(); 

separate problem smaller problems easier solve.

sorting logic :

public void insertionsort() {     int in, out;      (out = 1; out < getelementcount(); out++) {         state temp = states[out];         in = out;          while (in > 0 && compare(states[in-1], temp) > 0) {             states[in] = states[in - 1];             --in;         }         states[in] = temp;      } } 

comparision logic :

int compare( state a, state b ) {     int comparepopulation =  a.getpopulation().compareto(b.getpopulation());     if ( comparepopulation!=0 )         return comparepopulation;     else         return a.getregionnumber().compareto(b.getregionnumber()); } 

that sorts 1st population , region number.

here follows minimal, complete, , verifiable example

import java.util.concurrent.threadlocalrandom;  class test {  static int compare(state a, state b) {     int comparepopulation = a.getpopulation().compareto(b.getpopulation());     if (comparepopulation != 0) {         return comparepopulation;     } else {         return a.getregionnumber().compareto(b.getregionnumber());     } }  static void insertionsort() {     int in, out;      (out = 1; out < getelementcount(); out++) {         state temp = states[out];         in = out;          while (in > 0 && compare(states[in - 1], temp) > 0) {             states[in] = states[in - 1];             --in;         }         states[in] = temp;      } }  static class state {      private final integer population;     private final integer regionnumber;      public state(int population, int regionnumber) {         this.population = population;         this.regionnumber = regionnumber;     }      public static state getrandomstate() {         return new state(threadlocalrandom.current().nextint(10, 14),                 threadlocalrandom.current().nextint(1, 4));     }      public integer getregionnumber() {         return regionnumber;     }      public integer getpopulation() {         return population;     }      @override     public string tostring() {         return "p=" + population + " r=" + regionnumber;     } }  static int getelementcount() {     return states.length; }  static state[] states;  public static void main(string[] args) {     // create 10 random states     states = new state[10];     (int n = 0; n < 10; ++n) {         states[n] = state.getrandomstate();     }     // print them     system.out.println("----states unsorted----");     (state s : states) {         system.out.println(s);     }     // sort     insertionsort();     // print them sorted     system.out.println("----states sorted----");     (state s : states) {         system.out.println(s);     } } 

}

it generates 10 random states, prints them, sorts them , prints them sorted. in output can see states sorted 1st population , among same population "sub-sorted" region number.

----states unsorted----
p=10 r=1
p=10 r=2
p=12 r=1
p=11 r=2
p=11 r=2
p=13 r=1
p=12 r=2
p=13 r=1
p=10 r=2
p=12 r=1
----states sorted----
p=10 r=1
p=10 r=2
p=10 r=2
p=11 r=2
p=11 r=2
p=12 r=1
p=12 r=1
p=12 r=2
p=13 r=1
p=13 r=1


Comments